将结构体字段参数传递给函数

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

Passing struct field parameters to a function

问题

我有一个Message结构体和一个创建新Message并对其进行操作的函数。

type Message struct { 
    To string
    From string
    Body string
}

func Message() {
    newMessage := Message{Body: "test"}
    // 对newMessage进行操作
}

我想将结构体的参数传递给函数,就像这样(显然语法上不正确,但你能理解我的意思)。

func Message(/*params*/) {
    newMessage := Message{/*params*/}
    // 对newMessage进行操作
}

问题是,结构体参数本身没有类型,所以无法直接将它们传递给函数。我可以给函数传递一个map,并从中获取参数,但我希望保持message函数尽可能简单,避免像这样的操作:

Message(Message{/*params*/})

var params map[string]string
// 设置参数
Message(params)
英文:

I have a Message struct, and a function that creates a new Message and does something with it.

type Message struct { 
    To string
    From string
    Body string
}

func Message() {
    newMessage := Message{Body: "test"}
    // do something with newMessage
}

I'd like to pass the parameters to the struct into the function, kind of like this (obviously not syntactically correct, but you get the gist).

func Message(/*params*/) {
    newMessage := Message{/*params*/}
    // do something with newMessage
}

The problem is, struct parameters themselves don't have a type, so there's no way to give them directly to a function. I could probably give the function a map, and get the parameters out of there, but I want to keep using the message function as simple as possible, avoiding things like this:

Message(Message{/*params*/})

and

var params map[string]string
// set parameters
Message(params)

答案1

得分: 1

你想要实现什么目标?为什么结构参数本身没有类型?这段代码有什么问题?

package main

import "fmt"

type Message struct {
    To   string
    From string
    Body string
}

func NewMessage(to, from, body string) *Message {
    message := &Message{
        To:   to,
        From: from,
        Body: body,
    }
    // 对 message 进行一些操作
    return message
}

func main() {
    message := NewMessage(
        "message to",
        "message from",
        "message body",
    )
    fmt.Println("Message: ", *message)
}

输出结果:

Message:  {message to message from message body}
英文:

What exactly are you trying to accomplish? Why don't struct parameters themselves have a type. What is wrong with this?

package main

import "fmt"

type Message struct {
	To   string
	From string
	Body string
}

func NewMessage(to, from, body string) *Message {
	message := &Message{
		To:   to,
		From: from,
		Body: body,
	}
	// do something with message
	return message
}

func main() {
	message := NewMessage(
		"message to",
		"message from",
		"message body",
	)
	fmt.Println("Message: ", *message)
}

Output:

Message:  {message to message from message body}

答案2

得分: 0

直接传递一条消息:

func Send(msg Message) {
    // 处理消息
}
Send(Message{"to", "from", "body"})

如果你需要初始化其他属性,可以这样做:

type Message struct {
    id   int
    To   string
    From string
    Body string
}

func (this *Message) init() {
    if this.id == 0 {
        this.id = 1 // 在这里生成一个id
    }
}

func Send(msg Message) {
    msg.init()
    // 处理消息
}

Send(Message{
    To:   "to",
    From: "from",
    Body: "body",
})

不过,没有更多信息的情况下很难确定最佳方法。

英文:

Just pass in a message directly:

func Send(msg Message) {
    // do stuff with msg
}
Send(Message{"to","from","body"})

If there are additional properties you need to initialize you can do it like this:

type Message struct {
    id int
    To, From, Body string
}

func (this *Message) init() {
    if this.id == 0 {
        this.id = 1 // generate an id here somehow
    }
}

func Send(msg Message) {
    msg.init()
    // do stuff with msg
}

Send(Message{
    To: "to",
    From: "from",
    Body: "body",
})

Though it's hard to know the best approach without more information.

答案3

得分: -1

我认为你想要这样的东西,但这在Golang中不是有效的样式。

<i>

消息(收件人:'Ray',发件人:'Jack')

</i>

英文:

I think you want sth like this, but it's not valid style in Golang.
<i>

Message(To:'Ray',From:'Jack')

</i>

huangapple
  • 本文由 发表于 2013年12月27日 01:37:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/20788814.html
匿名

发表评论

匿名网友

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

确定