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

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)
}
主站蜘蛛池模板: 涟水县| 江西省| 秦皇岛市| 清涧县| 都江堰市| 荣成市| 山丹县| 临朐县| 湖北省| 九台市| 崇阳县| 兴山县| 鲜城| 永川市| 沂源县| 凤翔县| 南宁市| 华宁县| 潢川县| 扬中市| 阿克苏市| 安岳县| 宜兴市| 视频| 龙胜| 建阳市| 阿克| 土默特左旗| 新疆| 搜索| 威海市| 博湖县| 焦作市| 阿巴嘎旗| 桐乡市| 商南县| 栾城县| 托克托县| 金山区| 新安县| 龙游县|