What is the difference between * and &

package mainimport “fmt”func main() {var a string = “hello world”fmt.Println(a) // output: hello worldfmt.Println(&a) // output: 0xc00010a220 (won’t be this exactly)}

“&” Says, “get the address of the variable that follows”

package mainimport “fmt”func main() {var a string = “hello world”fmt.Println(a) // output: hello worldfmt.Println(&a) // output: 0xc00010a220 (won’t be this exactly)var b *string = &a //declare “b” as type “pointer to string”fmt.Println(b) // output: 0xc00010a220 (wont’ be this exactly)fmt.Println(*b) // output: hello world}

“*” says, “you are declaring that this variable holds a memory address to a string, or int or whatever type follows “*”. For example, “var a *int” declares that the variable “a” holds a memory address(pointer) to an int datatype.

”*” says “give me whatever the variable that follows is pointing to”. So your variable better be an actual memory address, otherwise you’ll get an error. It tells the system to use the value as a pointer and return whatever is at that address.

A yawn is a silent scream for coffee — Unknown Author