为字符串取消引用运算符

时间:2017-02-21 07:40:53

标签: 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 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"
}

1 个答案:

答案 0 :(得分:2)

*course = "Docker Extended"(也称为间接运算符)用于“取消引用”指针变量,取消引用指针使我们可以访问指针指向的值。
在这种情况下:string基本上你要对编译器说:存储course" Docker Extended"在内存位置If (!$this->session->userdata('login_type')) { redirect ('login', 'refresh') } 指的是。