dongshou1856 2017-02-21 07:40
浏览 19
已采纳

将值分配给字符串取消引用运算符

I'm new to go, pardon me if this is a regular question, How does the assignment to a string dereference operator works below?

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条回答 默认 最新

  • dpnv33177 2017-02-21 07:50
    关注

    * (also know as indirection operator) is used to “dereference” pointer variables, dereferencing a pointer gives us access to the value the pointer points to.
    In this case: *course = "Docker Extended" basically you are saying to the compiler: store the string "Docker Extended" in the memory location course refers to.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog