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

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)
}
主站蜘蛛池模板: 凉山| 鹤庆县| 甘洛县| 长宁区| 阳泉市| 肇庆市| 青岛市| 云阳县| 循化| 兴山县| 山丹县| 天气| 义乌市| 沂南县| 竹山县| 年辖:市辖区| 石楼县| 洪湖市| 娱乐| 吉木乃县| 兴隆县| 渭南市| 乐业县| 大余县| 英德市| 明溪县| 凤阳县| 巴林右旗| 大关县| 灌南县| 本溪| 云安县| 新民市| 宿迁市| 成都市| 邢台市| 佛山市| 乡宁县| 静宁县| 绥芬河市| 彭阳县|