如何在Go结构体中初始化成员

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

How to initialize members in Go struct

问题

我是新手,对Golang中的分配使我发疯:

import "sync"

type SyncMap struct {
        lock *sync.RWMutex
        hm map[string]string
}
func (m *SyncMap) Put (k, v string) {
        m.lock.Lock()
        defer m.lock.Unlock()

        m.hm[k] = v, true
}

然后,我只是调用:

sm := new(SyncMap)
sm.Put("Test", "Test")

此时我会得到一个空指针恐慌。

我通过使用另一个函数来解决这个问题,并在new()之后立即调用它:

func (m *SyncMap) Init() {
        m.hm = make(map[string]string)
        m.lock = new(sync.RWMutex)
}

但我想知道是否有可能摆脱这个样板初始化的方式?

英文:

I am new to Golang so allocation in it makes me insane:

import "sync"

type SyncMap struct {
        lock *sync.RWMutex
        hm map[string]string
}
func (m *SyncMap) Put (k, v string) {
        m.lock.Lock()
        defer m.lock.Unlock()

        m.hm[k] = v, true
}

and later, I just call:

sm := new(SyncMap)
sm.Put("Test, "Test")

At this moment I get a nil pointer panic.

I've worked around it by using another one function, and calling it right after new():

func (m *SyncMap) Init() {
        m.hm = make(map[string]string)
        m.lock = new(sync.RWMutex)
}

But I wonder, if it's possible to get rid of this boilerplate initializing?

答案1

得分: 70

你只需要一个构造函数。一个常用的模式是

func NewSyncMap() *SyncMap {
    return &SyncMap{hm: make(map[string]string)}
}

如果你的结构体内有更多的字段,可以在这个构造函数中启动一个goroutine作为后台,或者注册一个finalizer来完成所有操作。

func NewSyncMap() *SyncMap {
    sm := SyncMap{
        hm: make(map[string]string),
        foo: "Bar",
    }

    runtime.SetFinalizer(sm, (*SyncMap).stop)

    go sm.backend()

    return &sm
}
英文:

You just need a constructor. A common used pattern is

func NewSyncMap() *SyncMap {
    return &SyncMap{hm: make(map[string]string)}
}

In case of more fields inside your struct, starting a goroutine as backend, or registering a finalizer everything could be done in this constructor.

func NewSyncMap() *SyncMap {
    sm := SyncMap{
        hm: make(map[string]string),
        foo: "Bar",
    }

    runtime.SetFinalizer(sm, (*SyncMap).stop)

    go sm.backend()

    return &sm
}

答案2

得分: 11

'Mue'的解决方案不起作用,因为互斥锁没有初始化。以下修改后的代码可以正常工作:

package main

import "sync"

type SyncMap struct {
        lock *sync.RWMutex
        hm map[string]string
}

func NewSyncMap() *SyncMap {
    	return &SyncMap{lock: new(sync.RWMutex), hm: make(map[string]string)}
}

func (m *SyncMap) Put (k, v string) {
        m.lock.Lock()
        defer m.lock.Unlock()
        m.hm[k] = v
}

func main() {
	sm := NewSyncMap()
	sm.Put("Test", "Test")
}

http://play.golang.org/p/n-jQKWtEy5

英文:

The solution of 'Mue' doesn't work since the mutex is not initialized. The following modification works:

package main

import "sync"

type SyncMap struct {
        lock *sync.RWMutex
        hm map[string]string
}

func NewSyncMap() *SyncMap {
    	return &SyncMap{lock: new(sync.RWMutex), hm: make(map[string]string)}
}

func (m *SyncMap) Put (k, v string) {
        m.lock.Lock()
        defer m.lock.Unlock()
        m.hm[k] = v
}

func main() {
	sm := NewSyncMap()
	sm.Put("Test", "Test")
}

http://play.golang.org/p/n-jQKWtEy5

答案3

得分: 5

好的,以下是翻译好的内容:

deamon的发现很好。Mue可能在考虑将锁作为值而不是指针的更常见的模式。由于Mutex的零值是一个可立即使用的未锁定的Mutex,它不需要初始化,并且将其作为值包含是常见的。进一步简化,您可以通过省略字段名来嵌入它。然后,您的结构体将获得Mutex的方法集。请参见此工作示例,http://play.golang.org/p/faO9six-Qx。此外,我去掉了对defer的使用。在某种程度上,这是一种偏好和编码风格的问题,但由于它确实有一些小的开销,我倾向于在小函数中不使用它,特别是如果没有条件代码。

英文:

Good catch by deamon. Mue was possibly thinking of the more common pattern of including the lock as a value rather than a pointer. Since the zero value of a Mutex is a ready-to-use unlocked Mutex, it requires no initialization and including one as a value is common. As a further simplification, you can embed it by omitting the field name. Your struct then acquires the method set of the Mutex. See this working example, http://play.golang.org/p/faO9six-Qx. Also I took out the use of defer. To some extent it's a matter of preference and coding style, but since it does have a small overhead, I tend not to use it in small functions, especially if there is no conditional code.

huangapple
  • 本文由 发表于 2010年12月21日 19:58:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/4498998.html
匿名

发表评论

匿名网友

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

确定