Golang, call method from struct without variable

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

Golang, call method from struct without variable

问题

在Go语言中,不能直接使用结构体类型来调用方法,必须通过结构体变量来调用方法。所以在你的代码中,models.MyStruct.GetSomeAdditionalData()是无法工作的。

如果你想调用GetSomeAdditionalData()方法,你需要先创建一个结构体变量,然后通过该变量来调用方法,就像你在main()函数中所做的那样:

var variable models.MyStruct
fmt.Println(variable.GetSomeAdditionalData()) // 这样可以工作

至于如何为结构体添加数据,你可以通过结构体变量的字段来实现。例如,你可以为MyStruct结构体添加一个data字段,并在需要的时候给它赋值:

type MyStruct struct {
  id   int
  name string
  data string // 新增的字段
}

func main() {
  var variable models.MyStruct
  variable.data = "additional data string" // 给data字段赋值
  fmt.Println(variable.GetSomeAdditionalData()) // 可以工作了
}

希望这可以帮助到你!如果你还有其他问题,请随时提问。

英文:

Is it possible to call method from struct without variable with this struct type?

//models.go
type MyStruct struct {
  id int
  name string
}

func (s MyStruct) GetSomeAdditionalData() string {
  return "additional data string"
}

//app.go
func main() {
  fmt.Println(models.MyStruct.GetSomeAdditionalData()) // not works

  var variable models.MyStruct
  fmt.Println(variable.GetSomeAdditionalData()) // it worked
}

Or maybe Go have other method to add some data for struct?

Or maybe I select wrong way to do it? Golang, call method from struct without variable

答案1

得分: 12

你可以使用结构体字面量或nil指针。

MyStruct{}.GetSomeAdditionalData()
(*MyStruct)(nil).GetSomeAdditionalData()
英文:

You can use a struct literal or a nil pointer.

MyStruct{}.GetSomeAdditionalData()
(*MyStruct)(nil).GetSomeAdditionalData()

答案2

得分: 1

你可以这样说。MyStruct.GetSomeAdditionalData() 是一个被调用的方法表达式,你必须提供类型为 MyStruct 的第一个参数。参数可以是匿名复合字面量 MyStruct.GetSomeAdditionalData(MyStruct{})
这里有一个可工作的示例:https://play.golang.org/p/Wc_DjqnpLC。但是所有这些看起来都不太合理。

英文:

To say you can. MyStruct.GetSomeAdditionalData() is called method expression and you must provide first argument of type MyStruct to that call. Argument can be anonymous composite literal MyStruct.GetSomeAdditionalData(MyStruct{}).
Here is working example https://play.golang.org/p/Wc_DjqnpLC . But all that looks not very sensible.

答案3

得分: 0

你可以定义一个没有接收器的包函数。它与方法不同,因为方法需要一个接收器。

func GetSomeAdditionalData() string {
  return "additional data string"
}

你可以直接调用它,而不需要MyStruct的任何实例(因为你无论如何都不需要MyStruct的数据):

func main() {
  fmt.Println(models.GetSomeAdditionalData())
  fmt.Println(GetSomeAdditionalData())
}

(如果你已经在models包中,第二种形式也可以使用)

英文:

You can define a package function (without any receiver).
It differs from a method, as a method needs a receiver.

func GetSomeAdditionalData() string {
  return "additional data string"
}

Which you can call directly, without any instance of the struct MyStruct needed (since you don't need any of MyStruct data anyway):

func main() {
  fmt.Println(models.GetSomeAdditionalData())
  fmt.Println(GetSomeAdditionalData())

(the second form works if you are in the package models already)

huangapple
  • 本文由 发表于 2015年1月25日 03:48:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/28129535.html
匿名

发表评论

匿名网友

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

确定