# 函式 function

### Higher-order functions

• takes one or more functions as arguments
• returns a function as its result

Passing functions as arguments to other functions

Returning functions from other functions

### 初始化函式 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.

### 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.

# packages

• 執行檔 (executable)
• 函式庫 (library)

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.

### Custom Package

• 記得檔案要放在 \$GOPATH/src 底下，預設會去抓這底下的 src/geometry/rectangle
• 在 rectangle 資料夾底下的檔案，package 都必須是 rectangle
• 要 export 的變數都必須是大寫開頭，如以下 Area Diagonal，改成 area diagonal 就會變成 private method

### init function

1. Package level variables are initialised first
2. 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.

import 其他 package 時，就會執行該 package init(){}

• import 只能指定資料夾，無法指定檔案
• 同一個資料夾底下只能有一個 package，指的是 test 資料夾裡面所有的 package 都會是 test