Go - Declare Variables


Declaration
  1. Declare package
  2. Declare import
  3. var/const/func/type can be declared in any order

Variables
Declare variables with the same type and different types
Declare variables by method return multiple values
Short Variable Declaration
Use pattern "variables := expression" can declare variables.
package main
import (
       "fmt"
)
func main() {
       i, j := 1, 2
       fmt.Printf("%d, %d", i, j)
}
Short variable declaration expression can return multiple values
package main
import (
       "fmt"
)
func main() {
       q, k := multiReturn()
       fmt.Printf("%d, %d", q, k)
}
func multiReturn() (int, int) {
       return 3, 4
}
Must declare at least one new variable!
package main
import (
       "fmt"
)

// Declare new variable "q"
func main() {
       i, j := 1, 2
       j, q := i, j 
       fmt.Printf("i=%d,j=%d,q=%d", i, j, q)
}
Otherwise it will compile error



沒有留言:

張貼留言

別名演算法 Alias Method

 題目 每個伺服器支援不同的 TPM (transaction per minute) 當 request 來的時候, 系統需要馬上根據 TPM 的能力隨機找到一個適合的 server. 雖然稱為 "隨機", 但還是需要有 TPM 作為權重. 解法 別名演算法...