目录:
Go措辞的那些坑
Golang中函数被看做是值,函数值不可以比较,也不可以作为map的key

叨教以下代码能编译通过吗?
import ("fmt")func main(){array := make(map[int]func ()int)array[func()int{ return 10}()] = func()int{return 12}fmt.Println(array)}
答案:
可以正常编译通过。
稍作改动,改为如下的情形,还能编译通过吗?
import ("fmt")func main(){array := make(map[func ()int]int)array[func()int{return 12}] = 10fmt.Println(array)}
答案:
不能编译通过。
在Go措辞中,函数被看做是第一类值:(first-class values):函数和其他值一样,可以被赋值,可以通报给函数,可以从函数返回。也可以被当做是一种“函数类型”。例如:有函数func square(n int) int { return n n },那么就可以赋值f := square,而且还可以fmt.Println(f(3))(将打印出“9”)。Go措辞函数有两点很特殊:
函数值类型不能作为map的key函数值之间不可以比较,函数值只可以和nil作比较,函数类型的零值是nil匿名函数浸染域陷阱请看下列代码输出什么?
import ("fmt")func main(){var msgs []func()array := []string{"1", "2", "3", "4",}for _, e := range array{msgs = append(msgs, func(){fmt.Println(e)})}for _, v := range msgs{v()}}
答案:
4444
在上述代码中,匿名函数中记录的是循环变量的内存地址,而不是循环变量某一时候的值。
想要输出1、2、3、4须要改为:
import ("fmt")func main(){var msgs []func()array := []string{"1", "2", "3", "4",}for _, e := range array{elem := emsgs = append(msgs, func(){fmt.Println(elem)})}for _, v := range msgs{v()}}
实在就加了条elem := e看似多余,实在不,这样一来,每次循环后每个匿名函数中保存的就都是当时局部变量elem的值,这样的局部变量定义了4个,每次循环天生一个。
[3]int 和 [4]int 不算同一个类型请看一下代码,叨教输出true还是false
import ( "fmt" "reflect")func main(){ arrayA := [...]int{1, 2, 3} arrayB := [...]int{1, 2, 3, 4} fmt.Println(reflect.TypeOf(arrayA) == reflect.TypeOf(arrayB))}
答案是:
false
数组长度是数组类型的一个组成部分,因此[3]int和[4]int是两种不同的数组类型。
数组还可以指定一个索引和对应值的办法来初始化。例如:
func main() { var sampleMap map[string]int sampleMap = map[string]int { "test1":1, } sampleMap["test"] = 1 fmt.Println(sampleMap)}
会输出:
map[test1:1 test:1]
有点像PHP数组的觉得,但是又不一样:arrayA的长度是多少呢?
map[test1:1 test:1]
答案是:
map[test1:1 test:1]
没错,定义了一个数组长度为4的数组,指定索引的数组长度和末了一个索引的数值干系,例如:r := [...]int{99:-1}就定义了一个含有100个元素的数组r,末了一个元素输出化为-1,其他的元素都是用0初始化。
不能对map中的某个元素进行取地址&操作map[test1:1 test:1]
map中的元素不是一个变量,不能对map的元素进行取地址操作,禁止对map进行取地址操作的缘故原由可能是map随着元素的增加map可能会重新分配内存空间,这样会导致原来的地址无效
当map为nil的时候,不能添加值map[test1:1 test:1]
输出报错:
map[test1:1 test:1]
必须利用make或者将map初始化之后,才可以添加元素。
以上代码可以改为:
map[test1:1 test:1]
可以精确输出:
map[test1:1 test:1]
&dilbert.Position和(&dilbert).Position是不同的
&dilbert.Position相称于&(dilbert.Position)而非(&dilbert).Position
请看例子:
叨教输出什么?
func main(){ type Employee struct { ID int Name string Address string DoB time.Time Position string Salary int ManagerID int } var dilbert Employee dilbert.Position = "123" position := &dilbert.Position fmt.Println(position)}
输出:
0xc42006c220
输出的是内存地址
修正一下,把&dilbert.Position改为(&dilbert).Position
func main(){ type Employee struct { ID int Name string Address string DoB time.Time Position string Salary int ManagerID int } var dilbert Employee dilbert.Position = "123" position := &dilbert.Position fmt.Println(position)}
输出:
123
Go措辞中函数返回的是值的时候,不能赋值
请看下面例子:
type Employee struct { ID int Name string Address string DoB time.Time Position string Salary int ManagerID int}func EmployeeByID(id int) Employee { return Employee{ID:id}}func main(){ EmployeeByID(1).Salary = 0}
叨教能编译通过吗?
运行,输出报错:cannot assign to EmployeeByID(1).Salary
在本例子中,函数EmployeeById(id int)返回的是值类型的,它的取值EmployeeByID(1).Salary也是一个值类型;值类型是什么观点?值类型便是和赋值语句var a = 1或var a = hello world等号=右边的1、Hello world是一个观点,他是不能够被赋值的,只有变量能够被赋值。
修处死式如下:
type Employee struct { ID int Name string Address string DoB time.Time Position string Salary int ManagerID int}func EmployeeByID(id int) Employee { return Employee{ID:id}}func main(){ var a = EmployeeByID(1) a.Salary = 0}
这就可以编译通过了
在声明方法时,如果一个类型名称本身便是一个指针的话,不许可涌如今方法的吸收器中请看下面的例子,叨教会编译通过吗?
import ("fmt")type littleGirl struct{Name stringAge int}type girl littleGirlfunc(this girl) changeName(name string){this.Name = name}func main(){littleGirl := girl{Name:"Rose", Age:1}girl.changeName("yoyo")fmt.Println(littleGirl)}
答案:
不能编译通过,会提示“invalid receiver type girl(girl is a pointer type)”
Go措辞中规定,只有类型(Type)和指向他们的指针(Type)才是可能会涌如今吸收器声明里的两种吸收器,为了避免歧义,明确规定,如果一个类型名本身便是一个指针的话,是不许可涌如今吸收器中的。
函数许可nil指针作为参数,大概可用nil作为方法的吸收器请看下面的例子,叨教能编译通过吗?
import ("fmt")type littleGirl struct{Name stringAge int}func(this littleGirl) changeName(name string){fmt.Println(name)}func main(){little := littleGirl{Name:"Rose", Age:1}little = nillittle.changeName("yoyo")fmt.Println(little)}
答案:
不能编译通过,显示"cannot use nil as type littleGirl in assignment"
Go措辞中,许可方法用nil指针作为其吸收器,大概可函数将nil指针作为参数。而上述代码中的littleGirl不是指针类型,改为littleGirl,然后变量little赋值为&littleGirl{Name:"Rose", Age:1}就可以编译通过了。并且,nil对付工具来说是合法的零值的时候,比如map或者slice,也可以编译通过并正常运行。
Golang的韶光格式化不同于PHP的date("Y-m-d H:i:s", time()),Golang的格式化奇葩的很,不能利用诸如Y-m-d H:i:s的东西,而是利用2006-01-02 15:04:05这个韶光的格式,请记住这个韶光,听说这是Golang的出身韶光。
time := time.Now()time.Format("20060102") //相称于Ymdtime.Format("2006-01-02")//相称于Y-m-dtime.Format("2006-01-02 15:04:05")//相称于Y-m-d H:i:stime.Format("2006-01-02 00:00:00")//相称于Y-m-d 00:00:00