background
Google engineer Valentin Deleplace came up with a question about variable initialization. I thought it was very simple, but I didn't expect the correct answer rate to be less than 30%. I took it out and shared it with you.
topic
// quiz.go
package main
import "fmt"
func main() {
var a *int
*a = 5.0
fmt.Println(*a)
}
- A:
5
- B:
5.0
- C:
panic
- D: compile error
Parse
This question mainly examines two knowledge points:
- Variable zero value. In the title
a
is a pointer type variable,var a *int
this line of code does not initialize the value of the variablea
a
is zero, the zero value of a pointer isnil
, so the value of ---cfd19debc75342c35a50ef7ccf692bb7a
isnil
. Variable initialization assignment. If assigning a variable of type
int
to a floating-point number5.0
is legal, because5.0
is an untyped float constant, which can be converted without loss of precision for5
. If the assignment is5.1
it is illegal, because the precision after the decimal point is lost, and the compilation error is as follows:./quiz1.go:8:7: cannot use 5.1 (untyped float constant) as int value in assignment (truncated)
所以本题答案是C
,编译的时候不会报错,但是运行的a
的值是nil
, nil
Doing *
operation will trigger a panic. The specific content of the panic is: panic: runtime error: invalid memory address or nil pointer dereference
.
thinking questions
Topic 1:
// quiz1.go
package main
import "fmt"
func main() {
var a *int = new(int)
*a = 5.0
fmt.Println(*a)
}
Topic 2:
// quiz2.go
package main
import "fmt"
func main() {
var a *int = new(int)
var b float32 = 5.0
*a = b
fmt.Println(*a)
}
Topic 3:
// quiz3.go
package main
import "fmt"
func main() {
var a *int = new(int)
*a = 5.1
fmt.Println(*a)
}
If you want to know the answer, you can send a message to the init
to get the answer.
Summarize
The zero values of different types of variables in the Go language are different. We summarize the zero values of various types of variables:
Numeric: The zero value of all numeric types is 0
- Integer, zero value is 0. byte, rune, uintptr are also integer types, so the zero value is also 0.
- floating point number, zero value is 0
- Complex numbers, zero value is 0+0i
- Variables of integer type can be assigned with
untyped float constant
, as long as the precision is not lost.
- bool, zero value is false
- String, zero value is the empty string ""
Pointer: var a *int, zero value is nil
num := 100 var a * int = &num
slice: var a[]int, zero value is nil
var a []int = []int{1,2} list := [6]int{1,2} //size为6的数组,前面2个元素是1和2,后面的是默认值0
map: var a map[string] int, zero value is nil
dict := map[string] int{"a":1, "b":2}
function: var a func(string) int, zero value is nil
function := func(str string) string { return str } result := function("hello fans") fmt.Println("result=", result)
Structure: var instance Struct, the zero value of each field in the structure is the zero value of the corresponding type
type Circle struct { redius float64 } var c1 Circle c1.radius = 10.00
channel: var a chan int, channel channel, zero value is nil
var a chan int = make(chan int) var b = make(chan string) c := make(chan bool)
interface: var a interface_type, interface interface, zero value is nil
type Animal interface { speak() } type Cat struct { name string age int } func(cat Cat) speak() { fmt.Println("miao...") } // 定义一个接口变量a var a Animal = Cat{"gaffe", 1} a.speak() // miao...
open source address
Articles and sample code are open sourced on GitHub: Beginner, Intermediate, and Advanced Tutorials in Go .
Official account: coding advanced. Follow the official account to get the latest Go interview questions and technology stacks.
Personal website: Jincheng's Blog .
Zhihu: Wuji .
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。