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

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)
}
主站蜘蛛池模板: 巴彦淖尔市| 红桥区| 阜平县| 高唐县| 乐清市| 当阳市| 合川市| 北海市| 泽库县| 巢湖市| 云龙县| 高州市| 炎陵县| 大悟县| 武宁县| 绍兴市| 土默特右旗| 丰顺县| 黄山市| 平罗县| 新晃| 嘉禾县| 仲巴县| 大悟县| 聂拉木县| 松溪县| 泸西县| 东阳市| 洱源县| 嘉义市| 禹城市| 五常市| 杭州市| 镶黄旗| 晴隆县| 靖边县| 盈江县| 长乐市| 介休市| 淳化县| 柳江县|