dongpao1921 2016-06-27 10:36
浏览 22
已采纳

在Goroutine中填充此地图时,为什么该地图为空?

type driver struct {
    variables map[string]string
}

var Drivers []driver

func main() {

    driver := driver{
        variables: make(map[string]string),
    }
    Drivers = append(Drivers, driver)

    driver.variables = make(map[string]string) // Commenting this line makes it work, too

    done := make(chan bool) 
    go driver.populate(done)

    <-done

    fmt.Print(Drivers[0].variables)
}

func (this *driver) populate(done chan bool) {
    time.Sleep(500 * time.Millisecond)
    this.variables["a"] = "b"
    done <- true
}

I expected:

map[a:b]

Actual result:

map[]

Playground

  • 写回答

3条回答 默认 最新

  • douwuli4512 2016-06-27 11:37
    关注

    The problem is simple: you have a slice of drivers:

    var Drivers []driver
    

    Note that Drivers is a slice of some struct type, not a slice of pointers!

    When you append something (or you assign a value to one of its elements):

    Drivers = append(Drivers, driver)
    

    That makes a copy of the value appended (or assigned)! So when you do this later:

    driver.variables = make(map[string]string)
    

    It will set a new map value to driver.variables, but that is distinct from the value stored in Drivers (more precisely at Drivers[0]).

    Later you populate driver.variables, but you print Drivers[0].variables. They are 2 different struct values, with 2 different map values. Goroutines do not play a role here (they are properly synchronized so they shouldn't anyway).

    Would you print driver.variables:

    fmt.Print(driver.variables)
    

    You would see (try it on the Go Playground):

    map[a:b]
    

    If you comment out this line:

    driver.variables = make(map[string]string) // Commenting this line makes it work, too
    

    It would work, but only because even though you have 2 struct values, they have the same map value (same map header pointing to the same map data structure).

    You can also make it work if you call driver.populate() on the struct value Drivers[0] (and sticking to printing Drivers[0].variables):

    go Drivers[0].populate(done)
    
    // ...
    
    fmt.Print(Drivers[0].variables)
    

    Try this one on the Go Playground.

    And you can also make it work if Drivers is a slice of pointers:

    var Drivers []*driver
    
    // ...
    
    driver := &driver{
        variables: make(map[string]string),
    }
    

    Because driver and Driver[0] will be the same pointer (you will have only one struct value and one map value as the initial map is not accessible anymore). Try this on the Go Playground.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥30 求给定范围的全体素数p的(p-2)的连乘积
  • ¥15 VFP如何使用阿里TTS实现文字转语音?
  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页