- Security with Go
- John Daniel Leon
- 145字
- 2021-06-30 19:06:48
Pointer
Go provides a pointer type that stores the memory location where data of a specific type is stored. Pointers can be used to pass a struct to a function by reference without creating a copy. This also allows a function to modify an object in-place.
There is no pointer arithmetic allowed in Go. Pointers are considered safe because Go does not even define the addition operator on the pointer type. They can only be used to reference an existing object.
This example demonstrates basic pointer usage. It first creates an integer, and then creates a pointer to the integer. It then prints out the data type of the pointer, the address stored in the pointer, and then the value of data being pointed at:
package main
import (
"fmt"
"reflect"
)
func main() {
myInt := 42
intPointer := &myInt
fmt.Println(reflect.TypeOf(intPointer))
fmt.Println(intPointer)
fmt.Println(*intPointer)
}
推薦閱讀
- 下金蛋的數(shù)學(xué)問題
- 蟻群智能優(yōu)化方法及其應(yīng)用
- 世界是隨機的:大數(shù)據(jù)時代的概率統(tǒng)計學(xué)
- Blockchain Quick Reference
- 高等數(shù)學(xué)同步練習(xí)指導(dǎo)
- 越玩越聰明的印度數(shù)學(xué)和孫子算經(jīng)
- Mastering Ethereum
- 特殊函數(shù)概論習(xí)題解答
- 證明與布丁
- 數(shù)學(xué)女孩5:伽羅瓦理論
- 模糊數(shù)學(xué)基礎(chǔ)及應(yīng)用
- 現(xiàn)代啟發(fā)式優(yōu)化方法及其應(yīng)用
- 不可思議的自然對數(shù)
- 數(shù)學(xué)故事總動員
- 別萊利曼的趣味代數(shù)學(xué)