美文网首页Golang 学习笔记程序员
Golang:值类型与引用类型

Golang:值类型与引用类型

作者: 与蟒唯舞 | 来源:发表于2017-11-16 18:20 被阅读102次

Golang is a pass by value language, so any time we pass a value to a function either as a receiver or as an argument that data is copied in memory and so the function by default is always going to be working on a copy of our data structure. We can address this problem and modify the actual underlying data structure through the use of pointers and memory address.

值类型

先来看一段代码:

package main

import (
    "fmt"
)

type person struct {
    name string
}

func (p person) print() {
    fmt.Printf("%+v", p)
}

func (p person) updateName(newName string) {
    p.name = newName
}

func main() {
    jim := person{name: "Jim"}
    jim.updateName("Jimmy")
    jim.print()
}

运行结果:

{name:Jim}

没有达到我们期待的结果:

{name:Jimmy}

值传递意味着每当我们将某个值传递给函数时,golang 就会取那个值或那个结构。
为了达到我们的预期,修改源代码:

package main

import (
    "fmt"
)

type person struct {
    name string
}

func (p person) print() {
    fmt.Printf("%+v", p)
}

func (pointerToPerson *person) updateName(newName string) {
    (*pointerToPerson).name = newName
}

func main() {
    jim := person{name: "Jim"}
    jimPointer := &jim
    jimPointer.updateName("Jimmy")
    jim.print()
}

运行结果:

{name:Jimmy}

最后一点,小的改进:

func main() {
    jim := person{name: "Jim"}
    jim.updateName("Jimmy")
    jim.print()
}

pointer shortcut, go will automatically turn your variable of type person into pointer person for you.

引用类型

先看一段代码:

package main

import "fmt"

func updateSlice(s []string) {
    s[0] = "Bye"
}

func main() {
    mySlice := []string{"Hi", "There", "how", "are", "you?"}
    updateSlice(mySlice)
    fmt.Println(mySlice)
}

运行结果:

[Bye There how are you?]

运行结果为什么是这样?


When we make the slice of string golang internally is creating two separate data structures. What we call a function and pass my slice into it, golang is still behaving as a pass by value language. Even though the slice data structure is copied it is still pointing at the original array in memory, because the slice data structure and the array data structure are two separate elements in memory.

总结

Whenever you pass an integer, float, string, or struct into a function, golang will creates a copy of each argument, and these copies are used inside of the function.

相关文章

  • Golang:值类型与引用类型

    Golang is a pass by value language, so any time we pass a...

  • 【golang】值类型与引用类型

    golang中分为值类型和引用类型 值类型分别有:int系列、float系列、bool、string、数组和结构体...

  • Golang内置函数

    Golang内置类型和函数 内置类型值类型和引用类型的区别如下:值类型:内存中变量存储的是具体的值 比如: var...

  • javascript复习笔记一(变量类型与计算)

    变量类型:分为值类型与引用类型 一、值类型与引用类型 值类型:存储的是具体的值(undefined、Boolean...

  • JavaScript中的浅拷贝与深拷贝

    值类型与引用类型 谈浅拷贝与深拷贝之前,我们需要先理清一个概念,即值类型与引用类型。 什么是值类型与引用类型?这要...

  • Golang:数组与切片的复制

    在Golang中,数组是值类型而切片是引用类型。因此值的复制与切片的复制并不相同。 数组的复制 对于值类型的数组来...

  • 深拷贝与浅拷贝

    一.引用类型与值类型 我们都知道,js有两种基本类型,引用类型与值类型。引用类型的“=”只是拷贝了引用,而基本类型...

  • Note 3 Swift (1)

    Swift学习笔记 变量与常量 变量 常量 值类型与引用类型 值类型 引用类型 数组与字典 数组 字典

  • C#中数据到底存在堆上还是栈上

    C#的栈与堆 首先复习一下值类型与引用类型 1. 值类型与引用类型 值类型:基本数据类型([int,long,fl...

  • 装箱拆箱Day0817

    //值类型和引用类型之间的转换称为装箱与拆箱 //装箱是值类型转化为引用类型 //拆箱是引用类型转化为值类型 in...

网友评论

    本文标题:Golang:值类型与引用类型

    本文链接:https://www.haomeiwen.com/subject/upxrvxtx.html