// Long form var message string message = "Hello, Golang!" // Shorthand message := "Hello, Golang!"
声明和初始化多个变量
1 2 3 4 5 6 7 8
// Long form var a, b, c int a = 1 b = 2 c = 3 // Shorthand a, b, c := 1, 2, 3
交换变量
1 2 3 4 5 6 7 8 9
a, b := 1, 2 // Long form temp := a a = b b = temp // Shorthand a, b = b, a
Defer 函数调用
1 2 3 4 5 6 7 8 9 10
// Long form funccleanup() { // Cleanup logic } defer cleanup() // Shorthand deferfunc() { // Cleanup logic }()
检测 Map 中的数据是否存在
1 2 3 4 5 6 7 8 9 10
// Long form value, exists := myMap[key] if !exists { // Key doesn't exist in the map } // Shorthand if value, exists := myMap[key]; !exists { // Key doesn't exist in the map }
使用下标和值迭代切片
1 2 3 4 5 6 7 8 9
// Long form for i := 0; i < len(numbers); i++ { fmt.Println(i, numbers[i]) } // Shorthand for i, value := range numbers { fmt.Println(i, value) }
错误检测
1 2 3 4 5 6 7 8 9 10
// Long form result, err := someFunction() if err != nil { // Handle the error } // Shorthand if result, err := someFunction(); err != nil { // Handle the error }
创建一个变量的指针
1 2 3 4 5 6
// Long form var x int ptr := &x // Shorthand ptr := new(int)
匿名函数
1 2 3 4 5 6 7 8 9
// Long form funcadd(x, y int)int { return x + y } // Shorthand add := func(x, y int)int { return x + y }
import java.time.LocalDateTime; publicinterfaceTimeClient { voidsetTime(int hour, int minute, int second); voidsetDate(int day, int month, int year); voidsetDateAndTime(int day, int month, int year, int hour, int minute, int second); LocalDateTime getLocalDateTime(); }
# Cherry pick more than 1 SHA. # # This could be useful if you have a handful of commits that you want to bring over, # you'll likely want to order them with the oldest commit being first in the list. git cherry-pick <SHA> <SHA> # Edit the git commit message for the newly applied commit. # # This could be useful if want to customize the git commit message with extra context. git cherry-pick <SHA> --edit # Avoid automatically creating the commit which lets you edit the files first. # # This could be useful if you need to make manual code adjustments before committing, # such as applying a security patch which uses an older library with a different API. git cherry-pick <SHA> --no-commit