Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Go 将值赋给字符串取消引用运算符_Go - Fatal编程技术网

Go 将值赋给字符串取消引用运算符

Go 将值赋给字符串取消引用运算符,go,Go,我是新手,如果这是一个常规问题,请原谅,下面对字符串解引用运算符的赋值是如何工作的 package main import "fmt" func main() { course := "Docker Deep Dive" changeCourse(&course) } func changeCourse(course *string) { fmt.Println(course) // prints the memory address of cour

我是新手,如果这是一个常规问题,请原谅,下面对字符串解引用运算符的赋值是如何工作的

package main    

import "fmt"

func main() {
    course := "Docker Deep Dive"
    changeCourse(&course)
}

func changeCourse(course *string) {
    fmt.Println(course) // prints the memory address of course since it is a pointer
    fmt.Println(*course) // prints the value since * is dereferenceing the pointer

   // Issue
    *course = "Docker Extended" // *course is a string, how does the assignment works here.
    fmt.Println(*course) // prints "Docker Extended"
}
*
(也称为间接运算符)用于“取消引用”指针变量,取消引用指针使我们能够访问指针指向的值。

在这种情况下:
*course=“Docker Extended”
基本上你是在对编译器说:将
字符串存储在
course
引用的内存位置中。

简单地说,deference返回一个变量,以便我们可以访问和赋值@tinwor简单地说,间接运算符返回变量的值,而引用运算符返回变量的地址。在Go中,您还可以使用新的内置函数获取指针@srinivasdamam与任何解引用一样,如果指针为nil,这将导致nil指针解引用恐慌,因此请确保首先检查。