1. 程式人生 > >Go語言【第十四篇】:Go語言基礎總結

Go語言【第十四篇】:Go語言基礎總結

cal pro 結果 第十四 深入 得到 divider math XP

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
\n",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語言的深入部分,請期待...

Go語言【第十四篇】:Go語言基礎總結