Go语言学习查缺补漏ing Day2

Go语言学习查缺补漏ing Day2

一、函数返回参数命名的一个注意事项

请大家观察下面这个函数有什么问题吗?

func fun(x, y int) (s int, error) {
	return x * y, nil
}

虽然这个错误,在集成开发环境Goland中会有提示,但是其他的开发工具,比如VS Code就不知道会不会提示了。

image-20211120152703592

我们可以看到这个提示:函数有命名的返回参数,也有没有命名的返回参数。

这就说明函数有多个返回值参数时,如果你给一个参数命了名,那么其他参数也必须命名。而且如果给参数命名,那么必须给参数加上括号,无论参数个数是一个还是多个。这里给第一个参数命名为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)
}

显然是不能的,下面是报错信息:

image-20211120154559775

我们前面讲了,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怎么提示的吧。

image-20211120155228653

好像是不行吧。

这时我们就要使用...这种语法糖。

它有多个功能,其中的一个功能就是可以把切片打散进行传递。还有就是在定义函数时使用,可以接收任意个参数。

下面是运行结果:

image-20211120155606933

四、简短模式声明变量的限制

我们来看一看下面这一段代码,你觉得有没有什么问题?

package main

import "fmt"


var(
	two = 200
)
one := 100

func main() {
	fmt.Println(one,two)
}

是有问题的。

就得来谈一谈变量的简短模式声明有哪些限制:

  1. 必须使用显示初始化,也就是手工给予初值。
  2. 不能指定数据类型,编译器会根据你指定的初值自动推理变量的类型。
  3. 只能在函数内部使用简短模式来声明变量。

我们这里出现错误的原因就是触发了上述第三点限制:未在函数内部使用简短模式来声明变量。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ReganYue

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值