最佳实践,如何初始化自定义类型?

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

Best practices, How to initialize custom types?

问题

我还不习惯使用go的方式来做事情。
这里我有一个包装了BidiMap的类型ClientConnectorPool。我应该如何初始化这个类型?这样我就可以在之后添加到我的bidiMap中了。我尝试过的所有方法都很笨拙,我需要一些灵感,我能为它实现一些类似make(ClientConnectorPool)的函数吗?

在我的脑海中,它应该是这样的,但是我所有的解决方案都是为了避免空指针错误而写的15行代码 最佳实践,如何初始化自定义类型?

CC = make(ClientConnectorPool)
CC.Add("foo","bar")

代码:

package main

import ()

type ClientConnectorPool struct {
    Name string
	ConnectorList BidirMap
}

func (c ClientConnectorPool) Add(key, val interface{}){
     c.ConnectorList.Add(key,val)
}


type BidirMap struct {
	left, right map[interface{}]interface{}
}

func (m BidirMap) Add(key, val interface{}) {
	if _, inleft := m.left[key]; inleft {
		delete(m.left, key)
	}
	if _, inright := m.right[val]; inright {
		delete(m.right, val)
	}
	m.left[key] = val
	m.right[val] = key
}
英文:

I'm still not used to the go way of doing things.
Here I have the type ClientConnectorPool that wraps a BidiMap. How should I initialize this type? So that I can add to my bidiMap afterwords? All my attempt's of doing this sames hackish and I need inspiration, can I implement some sort om make(ClientConnectorPool) function for it?

In my head it should look like this but all my solutions is like 15 lines of code to avoid nil pointer errors 最佳实践,如何初始化自定义类型?

CC = make(ClientConnectorPool)
CC.Add("foo","bar")

Code:

package main

import ()

type ClientConnectorPool struct {
    Name string
	ConnectorList BidirMap
}

func (c ClientConnectorPool) Add(key, val interface{}){
     c.ConnectorList.Add(key,val)
}


type BidirMap struct {
	left, right map[interface{}]interface{}
}

func (m BidirMap) Add(key, val interface{}) {
	if _, inleft := m.left[key]; inleft {
		delete(m.left, key)
	}
	if _, inright := m.right[val]; inright {
		delete(m.right, val)
	}
	m.left[key] = val
	m.right[val] = key
}

答案1

得分: 3

你不能为make()定义自定义函数。make()只能用于切片、映射和通道(以及具有这些表示的自定义类型)。

Go的惯用方式是有一个NewClientConnectorPool函数(以下简称为NewPool),它创建并返回它。

func NewPool(name string) ClientConnectorPool {
  return ClientConnectorPool{
    Name: name,
    ConnectorList: BidirMap{
      left:  make(map[interface{}]interface{}),
      right: make(map[interface{}]interface{}),
    },
  }
}

你也可以有一个NewBidirMap函数来封装该结构的创建。

我不明白你为什么需要检查nil。make()不会返回nil,其余部分只是一个简单的结构体字面量。

英文:

You cannot define custom functions for make(). Make only works on slices, maps and channels (and custom types that have those representations).

Idiomatic Go would be to have a NewClientConnectorPool function (in the following abbreviated to NewPool) that creates and returns it.

func NewPool(name string) ClientConnectorPool {
  return ClientConnectorPool{
    Name: name,
    ConnectorList: BidirMap{
      left:  make(map[interface{}]interface{}),
      right: make(map[interface{}]interface{}),
    },
  }
}

You could also have a NewBidirMap function that wraps the creation of that struct.

I don't see where you need checks for nil though. make() won't return nil, and the rest is a simple struct literal.

huangapple
  • 本文由 发表于 2013年6月21日 02:41:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/17221347.html
匿名

发表评论

匿名网友

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

确定