dongshi2141
dongshi2141
2018-06-13 06:46

将int指针设置为int值Golang

  • pointers

I'm very new to Go and am trying to set a *int to point to a number, say 12345.

package main

import (
    "fmt"
    "reflect"
)

func main() {
    var guess *int
    fmt.Println(reflect.TypeOf(guess))
    *guess = 12345
    fmt.Println(guess)
}

But it's giving me the following error:

Type: *int
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x483c7d]

goroutine 1 [running]:
main.main()
    /home/aaron/projects/gopath/src/github.com/AaronNBrock/go-guess/main.go:16 +0x9d
exit status 2

I see that the error is with the *guess = 12345 since 12345, but I'm not sure what's causing it.

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

3条回答

为你推荐

换一换