zoukankan      html  css  js  c++  java
  • Go语言【第十四篇】:Go语言基础总结

    Go语言类型转换

    类型转换用于将一种数据类型的变量转换为另外一种类型的变量,Go语言类型转换基本格式如下:

    type_name(expression)
    

    type_name为类型,expression为表达式。
    实例
    以下实例中将整形转化为浮点型,并计算结果,将结果赋值给浮点型变量:

    package main
    
    import "fmt"
    
    func main() {
       var sum int = 17
       var count int = 5
       var mean float32
       
       mean = float32(sum)/float32(count)
       fmt.Printf("mean 的值为: %f
    ",mean)
    }
    

    以上实例输出结果为:

    mean 的值为: 3.400000
    

    Go语言接口

    Go语言提供了另外一种数据类型即接口,它把所有的具有共性的方法定义在一起,任何其他类型只要实现了这些方法就是实现了这个接口。
    实例

    /* 定义接口 */
    type interface_name interface {
       method_name1 [return_type]
       method_name2 [return_type]
       method_name3 [return_type]
       ...
       method_namen [return_type]
    }
    
    /* 定义结构体 */
    type struct_name struct {
       /* variables */
    }
    
    /* 实现接口方法 */
    func (struct_name_variable struct_name) method_name1() [return_type] {
       /* 方法实现 */
    }
    ...
    func (struct_name_variable struct_name) method_namen() [return_type] {
       /* 方法实现*/
    }
    

    实例

    package main
    
    import (
        "fmt"
    )
    
    type Phone interface {
        call()
    }
    
    type NokiaPhone struct {
    }
    
    func (nokiaPhone NokiaPhone) call() {
        fmt.Println("I am Nokia, I can call you!")
    }
    
    type IPhone struct {
    }
    
    func (iPhone IPhone) call() {
        fmt.Println("I am iPhone, I can call you!")
    }
    
    func main() {
        var phone Phone
    
        phone = new(NokiaPhone)
        phone.call()
    
        phone = new(IPhone)
        phone.call()
    
    }
    

    在上面的例子中,我们定义了一个接口Phone,接口里面有一个方法call()。然后我们在main函数里面定义了一个Phone类型变量,并分别为之赋值为NokiaPhone和IPhone。然后调用call()方法,输出结果如下:

    I am Nokia, I can call you!
    I am iPhone, I can call you!
    

    Go错误处理

    Go语言通过内置的错误接口提供了非常简单的错误处理机制,error类型是一个接口类型,这是它的定义:

    type error interface {
        Error() string
    }
    

    我们可以在编码中通过实现error接口类型来生成错误信息,函数通常在最后的返回值中返回错误信息,使用error.New可返回一个错误信息:

    func Sqrt(f float64) (float64, error) {
        if f < 0 {
            return 0, errors.New("math: square root of negative number")
        }
        // 实现
    }
    

    在下面的例子中,我们在调用Sqrt的时候传递的一个负数,然后就得到了non-nil的error对象,将此对象与nil比较,结果为true,所以fmt.PrintIn(fmt包在处理error时会调用Error方法)被调用,以输出错误,请看下面调用的示例代码:

    result, err := Sqrt(-1)
    
    if err != nil {
        fmt.PrintIn(err)
    }
    

    实例

    package main
    
    import (
        "fmt"
    )
    
    // 定义一个 DivideError 结构
    type DivideError struct {
        dividee int
        divider int
    }
    
    // 实现 `error` 接口
    func (de *DivideError) Error() string {
        strFormat := `
        Cannot proceed, the divider is zero.
        dividee: %d
        divider: 0
    `
        return fmt.Sprintf(strFormat, de.dividee)
    }
    
    // 定义 `int` 类型除法运算的函数
    func Divide(varDividee int, varDivider int) (result int, errorMsg string) {
        if varDivider == 0 {
            dData := DivideError{
                dividee: varDividee,
                divider: varDivider,
            }
            errorMsg = dData.Error()
            return
        } else {
            return varDividee / varDivider, ""
        }
    
    }
    
    func main() {
    
        // 正常情况
        if result, errorMsg := Divide(100, 10); errorMsg == "" {
            fmt.Println("100/10 = ", result)
        }
        // 当被除数为零的时候会返回错误信息
        if _, errorMsg := Divide(100, 0); errorMsg != "" {
            fmt.Println("errorMsg is: ", errorMsg)
        }
    
    }
    

    以上实例运行结果为:

    100/10 =  10
    errorMsg is:  
        Cannot proceed, the divider is zero.
        dividee: 100
        divider: 0
    

    至此,Go语言的基础部分已经分享完了,后面还会继续分享Go语言的深入部分,请期待...

  • 相关阅读:
    微信小程序
    js
    js
    uni
    uni/微信小程序
    uni/微信小程序
    ES6...扩展运算符(数组或类数组对象)
    微信小程序
    微信小程序
    玩转storm
  • 原文地址:https://www.cnblogs.com/love9527/p/8811279.html
Copyright © 2011-2022 走看看