2017-02-21 39 views
0

我是新来的,请原谅我,如果这是一个常规问题,那么如何赋值给字符串解引用操作符在下面工作?将值赋给字符串解引用操作符

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" 
} 

回答

2

*(也称为间接运算符)是用来“解引用”指针变量,解引用指针让我们访问的价值指针指向。
在这种情况下:*course = "Docker Extended"基本上你在说的是编译器:在内存位置course指的是存储string“Docker Extended”。

+0

简言之,deference返回一个变量,以便我们可以访问和分配值? @Tinwor –

+0

简单地说,间接运算符返回变量的值,而引用运算符返回变量的地址。在Go中,您还可以使用新的内置函数来获取指针。 @SrinivasDamam – Tinwor

+0

谢谢,这明确表示。 –