作者:ReganYue
来源:恒生LIGHT云社区
Go语言学习查缺补漏ing Day2
一、函数返回参数命名的一个注意事项
请大家观察下面这个函数有什么问题吗?
func fun(x, y int) (s int, error) { return x * y, nil }
虽然这个错误,在集成开发环境Goland中会有提示,但是其他的开发工具,比如VS Code就不知道会不会提示了。

我们可以看到这个提示:函数有命名的返回参数,也有没有命名的返回参数。
这就说明函数有多个返回值参数时,如果你给一个参数命了名,那么其他参数也必须命名。而且如果给参数命名,那么必须给参数加上括号,无论参数个数是一个还是多个。这里给第一个参数命名为s,而没有给第二个参数命名,所以有错误。
二、new()和make()有什么不同?
在Go SDK中,对new的描述是这样的:
// The new built-in function allocates memory. The first argument is a type, // not a value, and the value returned is a pointer to a newly // allocated zero value of that type. func new(Type) *Type
它是一个用于分配内存的内置函数,只有一个参数。如果使用这个函数,那么就会返回一个指向一块新开辟内存的指针,这块内存是第一个参数表示的类型的零值。
我们再来看一看make:
// 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. func make(t Type, size ...IntegerType) Type
我们可以了解,make也是分配内存的,但是只能给slice, map 或者 chan分配内存。而且这个make也不返回指针,而是返回你第一个参数代表的类型的值。
经过上面的介绍,我们再来看一看这段代码能否通过编译。
import "fmt" func main() { l := new([]int) l = append(l, 0) fmt.Println(l) }
显然是不能的,下面是报错信息:

我们前面讲了,new函数new出来的是指针,而指针是不能进行append操作的。所以我们建立slice, map 或者 chan最好使用make函数,而不要使用new函数。
三、切片追加切片问题
** 如果有两个切片,如何使用append把它们拼凑在一个切片里面呢?**
这样行不行?
package main import "fmt" func main() { slice := []int{8, 8, 8} append_slice := []int{2, 8} slice = append(slice, append_slice) fmt.Println(slice) }
看一看Goland怎么提示的吧。

好像是不行吧。
这时我们就要使用 ...
这种语法糖。
它有多个功能,其中的一个功能就是可以把切片打散进行传递。还有就是在定义函数时使用,可以接收任意个参数。
下面是运行结果:

四、简短模式声明变量的限制
我们来看一看下面这一段代码,你觉得有没有什么问题?
package main import "fmt" var( two = 200 ) one := 100 func main() { fmt.Println(one,two) }
是有问题的。
就得来谈一谈变量的简短模式声明有哪些限制:
- 必须使用显示初始化,也就是手工给予初值。
- 不能指定数据类型,编译器会根据你指定的初值自动推理变量的类型。
- 只能在函数内部使用简短模式来声明变量。
我们这里出现错误的原因就是触发了上述第三点限制:未在函数内部使用简短模式来声明变量。
评论(0)