在地图中为匿名结构值分配

huangapple go评论87阅读模式
英文:

Assign to anonymous struct value in map

问题

我正在使用go 1.3。

我如何访问map的匿名结构ValueType的字段?

package main

import "fmt"

type Words map[string]struct{
    pos   int
    n     int
}

func main() {
    w := make(Words)
    w["abc"].pos = 5 // 无法赋值

    fmt.Println(w)
}
英文:

I am using go 1.3.

How can I access the fields of the anonymous struct ValueType of the map ?

package main

import "fmt"
    
type Words map[string]struct{
    pos   int
    n     int
}

func main() {
    w := make(Words)
    w["abc"].pos = 5 // cannot assign
    
    fmt.Println(w)
}

答案1

得分: 9

例如,

package main

import "fmt"

type Words map[string]struct {
    pos int
    n   int
}

func main() {
    w := make(Words)
    v := w["abc"]
    v.pos = 5
    v.n = 42
    w["abc"] = v
    fmt.Println(w)
}

输出:

map[abc:{5 42}]
英文:

For example,

package main

import "fmt"

type Words map[string]struct {
	pos int
	n   int
}

func main() {
	w := make(Words)
	v := w["abc"]
	v.pos = 5
	v.n = 42
	w["abc"] = v
	fmt.Println(w)
}

Output:

map[abc:{5 42}]

答案2

得分: 2

你需要将一个值(你的结构体)分配给你的键:

type S struct {
    pos int
    n   int
}
type Words map[string]S

func main() {
    w := make(Words)
    s := S{pos: 1, n: 2}
    w["abc"] = s 
    fmt.Println(w)
}

在这个kbd示例中查看。

输出:

map[abc:{1 2}]

在"Go maps in action"中了解更多信息。

然后你可以检索你的值并进行分配:

sbis := w["abc"]
sbis.pos = 11
fmt.Println(sbis)

输出:

{11 2}

OneOfOne在他的示例中提出了一个getter函数,以便更快地分配pos,但如果需要,创建正确的值(即结构体的实例):

func (w Words) get(s string) (p *ps) {
    if p = w[s]; p == nil {
        p = &ps{}
        w[s] = p
    }
    return
}

这样就可以实现:

w := Words{}
w.get("abc").pos = 10
英文:

You need to assign a value (your struct) to your key:

type S struct {
	pos int
	n   int
}
type Words map[string]S

func main() {
	w := make(Words)
	s := S{pos: 1, n: 2}
	w["abc"] = s 
	fmt.Println(w)
}

See this <kbd>play.golang.org</kbd> example.

Output:

map[abc:{1 2}]

See more at "Go maps in action".

Then you can retrieve your value and assign:

sbis := w[&quot;abc&quot;]
sbis.pos = 11
fmt.Println(sbis)

Output:

{11 2}

In his example, OneOfOne proposes a getter in order to assign pos quicker, but creating if needed the right value (that is an instance of the struct):

func (w Words) get(s string) (p *ps) {
	if p = w
展开收缩
; p == nil { p = &amp;ps{} w
展开收缩
= p } return }

That allows:

w := Words{}
w.get(&quot;abc&quot;).pos = 10

答案3

得分: 1

你可以通过以下方式设置键的值。

package main

import "fmt"

func main() {
    w := make(map[string]struct {
        pos int
        n   int
    })
    w["abc"] = struct {
        pos int
        n   int
    }{
        pos: 5,
    }

    fmt.Println(w)
}

读取键的值更简单。

fmt.Println(w["abc"].pos)
英文:

You can set the value for a key this way.

package main

import &quot;fmt&quot;

func main() {
	w := make(map[string]struct {
		pos int
		n   int
	})
	w[&quot;abc&quot;] = struct {
		pos int
		n   int
	}{
		pos: 5,
	}

	fmt.Println(w)
}

Reading the value for a key is easier.

fmt.Println(w[&quot;abc&quot;].pos)

huangapple
  • 本文由 发表于 2014年7月29日 03:20:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/25002711.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定