官术网_书友最值得收藏!

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)
}
主站蜘蛛池模板: 汨罗市| 涟源市| 自贡市| 察隅县| 昭平县| 班玛县| 嘉鱼县| 静海县| 全州县| 尉犁县| 潢川县| 荣昌县| 沁水县| 大新县| 区。| 东台市| 桐柏县| 上杭县| 旺苍县| 甘泉县| 买车| 体育| 新龙县| 康定县| 临泉县| 海城市| 方城县| 山东省| 如皋市| 灵台县| 潮安县| 夏津县| 博兴县| 通道| 从江县| 邓州市| 敖汉旗| 锦州市| 东源县| 北安市| 华蓥市|