How the Go language assigns values to the *int32 type

Source: Internet
Author: User
Directly on the code:

package mainimport "fmt"func main() {    var num *int32    var i int32 = 5    // 针对*int32的,先创建一个i类型,然后取i的地址,赋值给*int32    num = &i    fmt.Println("=====>:\t", num)    fmt.Println("=====>:\t", *num)}

Operation Result:

=====>:  0xc042064080=====>:  5Process finished with exit code 0

The following form is not possible:

package mainimport "fmt"func main() {    var num *int32    *num = 5    fmt.Println("=====>:\t", num)    fmt.Println("=====>:\t", *num)}

Operation Result:

panic: runtime error: invalid memory address or nil pointer dereference[signal 0xc0000005 code=0x1 addr=0x0 pc=0x48b4ea]goroutine 1 [running]:main.main()    E:/Program/go2/goPath/src/xingej-go/xingej-go/xingej-go666/string/test.go:7 +0x2aProcess finished with exit code 2  

Reason?
The address of the constant 5 may not be taken

How the Go language assigns values to the *int32 type

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.