Skip to content

Latest commit

 

History

History
47 lines (33 loc) · 1.44 KB

01_Variables.md

File metadata and controls

47 lines (33 loc) · 1.44 KB

Variables

Problem 1

package main

import (
	"fmt"
)

func main() {
	var x int = 1
	var y *int
	var z [1]int
	z[0] += x
	*y = x + z[0]
	fmt.Println("y =", y)
}
ANSWER

A variable's value is retrieved by referring to the variable in an expression; it is the most recent value assigned to the variable. If a variable has not yet been assigned a value, its value is the zero value for its type.

If x is nil, an attempt to evaluate *x will cause a run-time panic.

  • よって、問題のコードではnilであるyに対して*yを評価しているので、panicが起こります。