英文:
goLang pass struct to function (args interface{})
问题
这是我的代码
http://play.golang.org/p/h0N4t2ZAKQ
package main
import (
"fmt"
"reflect"
)
type Msg struct {
Message string
}
func print(y interface{}) {
z, ok := y.(Msg)
fmt.Println(reflect.TypeOf(z))
fmt.Println("ok 的值为 ", ok)
if ok {
fmt.Println("Message 是 "+ z.Message)
}
}
func main() {
foo := new(Msg)
foo.Message="Hello"
fmt.Println("main 函数中的 Message 是 "+foo.Message)
print(foo)
}
当我运行它时,z.Message 没有打印出 Hello
不确定为什么。有人能解释一下吗?
提前感谢。
英文:
Here is my code
http://play.golang.org/p/h0N4t2ZAKQ
package main
import (
"fmt"
"reflect"
)
type Msg struct {
Message string
}
func print(y interface{}) {
z, ok := y.(Msg)
fmt.Println(reflect.TypeOf(z))
fmt.Println("Value of ok ", ok)
if ok {
fmt.Println("Message is "+ z.Message)
}
}
func main() {
foo := new(Msg)
foo.Message="Hello"
fmt.Println("Messege in main "+foo.Message)
print(foo)
}
When I run it z.Message does not print Hello
Not sure why. Can someone clarify?
Thanks in advance
答案1
得分: 0
你的程序中,foo
的类型是 *Msg
(指向 Msg 的指针),而不是 Msg
。你需要在 print
中将 y
强制转换为 *Msg
(http://play.golang.org/p/MTi7QhSVQz):
z, ok := y.(*Msg)
或者你可以将 foo
的类型设置为 Msg
(http://play.golang.org/p/XMftjVtzBk):
foo := Msg{Message: "Hello"}
或者
var foo Msg
foo.Message = "Hello"
英文:
Type of foo
in your program is *Msg
(pointer to Msg), not Msg
. You need to cast y
to *Msg
in print
(http://play.golang.org/p/MTi7QhSVQz):
z, ok := y.(*Msg)
Alternatively you can use Msg
type for foo
(http://play.golang.org/p/XMftjVtzBk):
foo := Msg{Message: "Hello"}
or
var foo Msg
foo.Message = "Hello"
答案2
得分: 0
如果你运行程序,你会注意到"ok"的值是false,这就是为什么if语句中的打印语句没有执行的原因。如果你从z中移除"ok",即将y.(Msg)改为ok := y.(Msg),你会看到Go在执行这个断言语句时抛出的错误。有了"ok",如果断言失败,Go不会发生恐慌,而是返回false。这就是你的情况。
断言失败的原因是,print方法中的类型是Msg(main.Msg),但传递的是指针,即*main.Msg。当你不使用"ok"时,你会看到这个错误。
所以一种方法是:
print(*foo)
或者
z, ok := y.(*Msg)
英文:
If you run your program, one thing that you will notice is that value of "ok" is false and that's the reason for your print statement in if not getting executed. If you remove "ok" from z, ok := y.(Msg), you will see the error that Go is throwing while executing this assert statement. With ok, Go will not panic and will return false if assertion fails. Which is happening in your case.
The reason for asserting failing is, expected, type in print method is Msg(main.Msg), but passed is pointer i.e. *main.Msg. You will see this error when you don't use "ok"
So one way is to
print(*foo)
Or
z, ok := y.(*Msg)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论