在Go语言中,如何为一个没有任何参数的类型创建一个构造函数

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

In go, how to create a constructor for a type without any arguments

问题

给定这个类型

type Response map[string]interface{}

我创建了一个方法NewResponse来填充默认值:

 func NewResponse() Response {
	resp := Response{"status": 200, "msg": "Added jobs to queue"}

	resp_metadata := make(map[string]string)
	resp_metadata["base"] = "api/v1"
	resp_metadata["self"] = "/crawler/jobs/add"
	resp["metadata"] = resp_metadata
	return resp
}

我想要像这样调用NewResponse(),但我希望改为Response.New(),所以方法签名应该是这样的

func (Response) New() Response {

但是我总是得到错误not enough arguments in call to Response.New

那么,如何实现这个呢?

英文:

Given this type

type Response map[string]interface{}

I created a method NewResponse which fills in the default values:

 func NewResponse() Response {
	resp := Response{"status": 200, "msg": "Added jobs to queue"}

	resp_metadata := make(map[string]string)
	resp_metadata["base"] = "api/v1"
	resp_metadata["self"] = "/crawler/jobs/add"
	resp["metadata"] = resp_metadata
	return resp
}

which i call like NewResponse() but I would like to do Response.New() instead, so the method signature should be like this

func (Response) New() Response {

but then I always get the error not enough arguments in call to Response.New.

So, how could this be implemented?

答案1

得分: 3

当你编写Go程序时,请使用惯用的Go语法。这样,其他人就能够阅读你的程序。例如,

package main

import "fmt"

type Response map[string]interface{}

func NewResponse() Response {
    metadata := map[string]string{
        "base": "api/v1",
        "self": "/crawler/jobs/add",
    }
    r := Response{
        "status":   200,
        "msg":      "Added jobs to queue",
        "metadata": metadata,
    }
    return r
}

func main() {
    resp := NewResponse()
    fmt.Println(resp)
}

输出结果:

map[status:200 msg:Added jobs to queue metadata:map[base:api/v1 self:/crawler/jobs/add]]
英文:

When you write Go programs, use idiomatic Go. Then, other people will be able to read your programs. For example,

package main

import "fmt"

type Response map[string]interface{}

func NewResponse() Response {
	metadata := map[string]string{
		"base": "api/v1",
		"self": "/crawler/jobs/add",
	}
	r := Response{
		"status":   200,
		"msg":      "Added jobs to queue",
		"metadata": metadata,
	}
	return r
}

func main() {
	resp := NewResponse()
	fmt.Println(resp)
}

Output:

map[status:200 msg:Added jobs to queue metadata:map[base:api/v1 self:/crawler/jobs/add]]

答案2

得分: 2

尽管这绝对不是Go的惯用方式,但你可以做一些像这样的事情

type Response map[string]interface{}

func (r *Response) New() {
  *r = make(map[string]interface{})
  (*r)["hello"] = "World"
  (*r)["high"] = 5
}

func main() {
  var r Response
  r.New()
  for k, v := range r {
    fmt.Printf("%s = %v\n", k, v)
  }
}

但实际上,使用func NewResponse() Response也没有问题。

英文:

Although it is definitely not idiomatic Go, you could do something like this:

type Response map[string]interface{}

func (r *Response) New() {
  *r = make(map[string]interface{})
  (*r)["hello"] = "World"
  (*r)["high"] = 5
}

func main() {
  var r Response
  r.New()
  for k, v := range r {
    fmt.Printf("%s = %v\n", k, v)
  }
}

But really, there's nothing wrong with func NewResponse() Response.

答案3

得分: 1

它不需要。Go语言没有构造函数。创建一个“空”对象就是创建该对象类型的零值

你试图做的是在一个已存在的Response对象上调用一个名为New的Response方法,该方法将返回一个(不同的)Response对象。

如果你需要创建一个空的Response对象,可以使用resp := Response{}resp := make(Response)

英文:

It doesn't. Go doesn't have constructors. To create an "empty" object is to create a zero value of the type of the object.

What you're trying to do is a Response method named New to be called on an existing Response object that would return a (different) Response object.

resp := Response{} or resp := make(Response) is fine if you need to create an empty Response.

huangapple
  • 本文由 发表于 2013年7月10日 23:28:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/17574770.html
匿名

发表评论

匿名网友

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

确定