Golang: panic: runtime error: invalid memory address or nil pointer dereference

in below code will show this error

panic: runtime error: invalid memory address or nil pointer dereference

package main

import "fmt"

func main() {
	var u *User
	u.Name = "nnnn"
}

type User struct {
	ID     string
	Name   string
	Hight  uint16
	Weight uint16
}

the reason why is that declaration only declare the variable, it's didn't give a memory address to the new variable.

To avoid this error, just new the variable, and it will be grant a memory address.

package main

func main() {
	var u *User
	u = &User{}
	u.Name = "nnnn"
}

type User struct {
	ID     string
	Name   string
	Hight  uint16
	Weight uint16
}