函式 function
1 2 3 4 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
|
variadic function
當有不確定的參數時,可用 ...
來代替
只能夠放在最後一個參數
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|
匿名函式 Anonymous function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
|
Defined function types
像定義 struct type 一樣,也可以定義 func type
1
|
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
Higher-order functions
- takes one or more functions as arguments
- returns a function as its result
Passing functions as arguments to other functions
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
Returning functions from other functions
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
閉包函式 Closure function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
|
初始化函式 Init function
- The init function should not have any return type and should not have any parameters
- Package level variables are initialised first
- init function is called next. A package can have multiple init functions (either in a single file or distributed across multiple files) and they are called in the order in which they are presented to the compiler.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
fibonacci
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
|
Practical use of first class functions
What are first class functions?
A language which supports first class functions allows functions to be assigned to variables, passed as arguments to other functions and returned from other functions. Go has support for first class functions.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
packages
1
|
|
宣告程式屬於哪個 package
,所有的 go 檔案都必須聲明,要 import 這個檔案時,就必須使用 test
這個名稱。
而 go 又分兩種專案
- 執行檔 (executable)
- 函式庫 (library)
執行檔一定要宣告為 main
套件
1 2 |
|
main 是一個比較特殊的 package,
Package main is special. It defines a standalone executable program, not a library. Within package main the function main is also special—it’s where execution of the program begins. Whatever main does is what the program does.
因此一定要有個 main
package 當作是程式入口
Custom Package
目錄結構
1 2 3 4 5 6 7 8 9 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
- 記得檔案要放在
$GOPATH/src
底下,預設會去抓這底下的src/geometry/rectangle
- 在 rectangle 資料夾底下的檔案,package 都必須是 rectangle
- 要 export 的變數都必須是大寫開頭,如以下
Area
Diagonal
,改成area
diagonal
就會變成 private method
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
init function
每個 package 都可以有多個 init(){}
,並且是一開始就會執行
1 2 |
|
執行的順序為
- Package level variables are initialised first
- init function is called next. A package can have multiple init functions (either in a single file or distributed across multiple files) and they are called in the order in which they are presented to the compiler.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
|
import 其他 package 時,就會執行該 package init(){}
有時候只是為了 init(){}
並不是真的要使用該 package 就可以用 _
來代替
1 2 3 |
|
- import 只能指定資料夾,無法指定檔案
- 同一個資料夾底下只能有一個 package,指的是 test 資料夾裡面所有的 package 都會是 test
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
|
參考文件: