<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                [toc] 函數使用 > 函數是Go語言里面的核心設計,它通過關鍵字func來聲明,它的格式如下。 # 1. 函數聲明 ``` func funcName(input1 type1, input2 type2) (output1 type1, output2 type2) { //這里是處理邏輯代碼 //返回多個值 return value1, value2 } ``` * 關鍵字func用來聲明一個函數funcName。 - 函數可以有一個或者多個參數,每個參數后面帶有類型,通過“,”分隔函數可以返回多個值。 - 返回值聲明了兩個變量output1和output2,如果你不想聲明也可以,就保留兩個類型聲明。 - 如果只有一個返回值且不聲明返回值變量,那么你可以省略“包括返回值”的括號 - 如果沒有返回值,就直接省略最后的返回信息。 - 如果有返回值,那么必須在函數的外層添加return語句。 # 2. 接收變參的函數 ``` func funcName(input ...type) (output1 type1, output2 type2) { //這里是處理邏輯代碼 //返回多個值 return output1 type1, output2 type2 } ``` > 注意,這些參數的類型全部是type。在函數體中,變量input是一個type的slice。 - 使用示例 ``` package main import "fmt" func main() { result := joinStr("h", "e", "l", "l", "o") fmt.Println(result) // hello } func joinStr(input ...string) (strRes string) { for _, v := range input { strRes += v } return } ``` # 3.參數傳遞 > Go 默認使用按值傳遞參數,也就是傳遞參數的副本。函數接收參數副本之后,在使用變量的過程中可能對副本的值進行更改,但不會影響到原來的變量。如果想要影響到原來的變量,則需要傳指針。<font color=red>需要注意的是:切片(slice)、字典(map)、接口(interface)、通道(channel)這樣的引用類型都是默認使用引用傳遞(即使沒有顯示的指出指針)</font> ## 3.1 按值傳遞使用示例 ``` package main import "fmt" func main() { str := "good morning" updateStr(str) fmt.Println(str) // good morning } func updateStr(str string) string { str = "good evening" return str } ``` ## 3.2 按引用使用示例 ``` package main import "fmt" func main() { str := "good morning" updateStr(&str) fmt.Println(str) // good evening } func updateStr(str *string) string { *str = "good evening" return *str } ``` # 4.延遲語句(defer)的使用 >Go語言中有種不錯的設計,即延遲(defer)語句,你可以在函數中添加多個defer語句。當函數執行到最后時,這些 defer 語句會按照逆序執行,最后該函數返回。特別是當你在進行一些打開資源的操作時,遇到錯誤需要提前返回,在返回前你需要關閉相應的資源,不然很容易造成資源泄露等問題。 ## 4.1 使用示例 ``` package main import ( "fmt" "os" "io/ioutil" ) func main() { filePath := "/Users/liuqh/Desktop/test.json" str := openFile(filePath) fmt.Println(str) //輸出以下內容 //defer 被調用了 //文件里的內容 } func openFile(filePath string) string { //打開文件 file, err := os.Open(filePath) if err != nil { fmt.Println("open file err") } //利用defer 關閉文件 defer testDefer(file) content, err := ioutil.ReadAll(file) if err != nil { fmt.Println("read file err") } return string(content) } func testDefer(file *os.File) { fmt.Println("defer 被調用了") file.Close() } ``` ## 4.2 函數中使用多個defer > 如果有一個函數中有多個地方調用defer,那么defer采用后進先出(逆序執行) ``` package main import ( "fmt" ) func main() { testDefer() // 5 4 3 2 1 } func testDefer() { for i := 1; i < 6; i++ { defer fmt.Println(i) } } ``` # 5.函數作為值、類型 >在Go語言中函數也是一種變量,我們可以通過type來定義它,它的類型就是所有擁有相同的參數,相同的返回值。 ``` go type typeName func(input1 inputType1,input2 inputType2[,...])(result1 resultType1 [, ...]) ``` ## 5.1 使用示例 ``` package main import "fmt" type testInt func(int) bool func main() { numSlice := []int{1, 2, 3, 4, 5, 6, 7, 8, 9} odd := filter(numSlice, isOdd) event := filter(numSlice, isEvent) fmt.Println(odd) // [2 4 6 8] fmt.Println(event) // [1 3 5 7 9] } func isOdd(num int) bool { if num%2 == 0 { return true } return false } func isEvent(num int) bool { if num%2 == 0 { return false } return true } func filter(arg []int, funcName testInt) []int { var result []int for _, v := range arg { if funcName(v) { result = append(result, v) } } return result } ``` # 6.匿名函數 既沒有名稱的函數 ## 6.1 使用示例 ``` package main import "fmt" func main() { f := func(a,b int) int{ i := a + b return i } fmt.Println(f(1,4)) // 5 } ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看