var n =0// 错误使用// func add() int {// n = n++ // syntax error: unexpected ++ at end of statement// return n++ // syntax error: unexpected ++ at end of statement// }funcadd()int{
n++// 单独成句return n
}
funcmakeSuffixFunc(suffix string)func(string)string{returnfunc(name string)string{if!strings.HasSuffix(name, suffix){return name + suffix
}return name
}}funcmain(){
jpgFunc :=makeSuffixFunc(".jpg")
txtFunc :=makeSuffixFunc(".txt")
fmt.Println(jpgFunc("test"))//test.jpg
fmt.Println(txtFunc("test"))//test.txt}
查看go内置函数定义:go doc builtin.make
package builtin // import "builtin"funcmake(t Type, size ...IntegerType) Type
The make built-in function allocates and initializes an object of type
slice,map, or chan(only). Like new, the first argument is a type, not a
value. Unlike new,make's return type is the same as the type of its
argument, not a pointer to it. The specification of the result depends on
the type:
Slice: The size specifies the length. The capacity of the slice is
equal to its length. A second integer argument may be provided to
specify a different capacity; it must be no smaller than the
length. For example, make([]int, 0, 10) allocates an underlying array
of size 10 and returns a slice of length 0 and capacity 10 that is
backed by this underlying array.
Map: An empty map is allocated with enough space to hold the
specified number of elements. The size may be omitted, in which case
a small starting size is allocated.
Channel: The channel's buffer is initialized with the specified
buffer capacity. If zero, or the size is omitted, the channel is
unbuffered.