内容简介:知识点golang中零值的概念划重点:客官请看下面代码:
知识点golang中零值的概念
整型的零值:0
浮点的零值:0
string的零值:""(字符串空)
byte, rune 的零值:0
数组[len]type的零值为: 长度为 len 每个元素的零值为 type 对应的零值。
例如: var c[4]int 的零值为 [0 0 0 0]
slice、map、channel和指针类型的零值:nil
划重点:
客官请看下面代码:
package main
import "fmt"
type Ball struct {
val int
}
type Person1 struct {
person1 *Ball
}
type Person2 struct {
person2 Ball
}
type Bar interface {
setVal(newVal int)
getVal() int
}
func (b Ball) getVal() int {
return b.val
}
func (b *Ball) setVal(newVal int) {
b.val = newVal
}
func main() {
var person1 Person1
//person1 := &Person1{&Ball{val: 10}}
fmt.Println(person1.person1.getVal())
person1.person1.setVal(1000)
fmt.Println(person1.person1.getVal())
//var person2 Person2
person2 := &Person2{Ball{val: 20}}
fmt.Println(person2.person2.getVal())
person2.person2.setVal(2000)
fmt.Println(person2.person2.getVal())
}
看下效果
panic: runtime error: invalid memory address or nil pointer dereference [signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x1091506] goroutine 1 [running]: main.main()
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:- golang的值类型,指针类型和引用类型&值传递&指针传递
- go 方法接受者 是指针类型和非指针类型的 区别
- golang中值类型/指针类型的变量区别总结
- 7. Go 语言数据类型:指针
- Golang研学:在用好Golang指针类型
- golang 指针类型引起的神奇 bug
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Data Structures and Algorithms
Alfred V. Aho、Jeffrey D. Ullman、John E. Hopcroft / Addison Wesley / 1983-1-11 / USD 74.20
The authors' treatment of data structures in Data Structures and Algorithms is unified by an informal notion of "abstract data types," allowing readers to compare different implementations of the same......一起来看看 《Data Structures and Algorithms》 这本书的介绍吧!