Golang 在结构之间切换
Golang switch between structs
我是 golang 的新手,我正在尝试创建一个函数,该函数基于它所使用的结构,将 return 使用 Sprintf
格式化的字符串
type Name struct {
Title string
First string
Last string
}
type Location struct {
Street string
City string
State string
Zip string
}
func Merge(m interface{}) string {
switch m.(type) {
case *Location:
return fmt.Sprintf("%s \n %s, %s %s", m.(*Location).Street, m.(*Location).City, m.(*Location).State, m.(*Location).Zip)
case *Name:
return fmt.Sprintf("%s. %s %s", m.(*Name).Title, m.(*Name).First, m.(*Name).Last)
}
return "Not Applicable"
}
fmt.Println(Merge(Location))
我收到来自 PrintLn
的“Not Applicable
”消息。在一个版本的代码中,我相信消息是“out of index
”。
在您的示例中,您试图将结构本身而不是结构的实例传递给函数。当我 运行 您的代码无法编译时。不过,我同意上面的评论,通过确保每个结构都满足 fmt.Stringer 接口可以更好地处理这个问题。
您的代码的固定版本:
package main
import "fmt"
type Name struct {
Title string
First string
Last string
}
type Location struct {
Street string
City string
State string
Zip string
}
func Merge(m interface{}) string {
switch m.(type) {
case Location:
return fmt.Sprintf("%s \n %s, %s %s", m.(Location).Street, m.(Location).City, m.(Location).State, m.(Location).Zip)
case Name:
return fmt.Sprintf("%s. %s %s", m.(Name).Title, m.(Name).First, m.(Name).Last)
}
return "Not Applicable"
}
func main() {
l := Location{
Street: "122 Broadway",
City: "New York",
State: "NY",
Zip: "1000",
}
fmt.Println(Merge(l))
}
使用fmt.String的版本:
package main
import "fmt"
type Name struct {
Title string
First string
Last string
}
func (n *Name) String() string {
return fmt.Sprintf("%s. %s %s", n.Title, n.First, n.Last)
}
type Location struct {
Street string
City string
State string
Zip string
}
func (l *Location) String() string {
return fmt.Sprintf("%s \n %s, %s %s", l.Street, l.City, l.State, l.Zip)
}
func main() {
l := &Location{
Street: "120 Broadway",
City: "New York",
State: "NY",
Zip: "1000",
}
fmt.Println(l)
n := &Name{
Title: "Mr",
First: "Billy",
Last: "Bob",
}
fmt.Println(n)
}
我是 golang 的新手,我正在尝试创建一个函数,该函数基于它所使用的结构,将 return 使用 Sprintf
格式化的字符串type Name struct {
Title string
First string
Last string
}
type Location struct {
Street string
City string
State string
Zip string
}
func Merge(m interface{}) string {
switch m.(type) {
case *Location:
return fmt.Sprintf("%s \n %s, %s %s", m.(*Location).Street, m.(*Location).City, m.(*Location).State, m.(*Location).Zip)
case *Name:
return fmt.Sprintf("%s. %s %s", m.(*Name).Title, m.(*Name).First, m.(*Name).Last)
}
return "Not Applicable"
}
fmt.Println(Merge(Location))
我收到来自 PrintLn
的“Not Applicable
”消息。在一个版本的代码中,我相信消息是“out of index
”。
在您的示例中,您试图将结构本身而不是结构的实例传递给函数。当我 运行 您的代码无法编译时。不过,我同意上面的评论,通过确保每个结构都满足 fmt.Stringer 接口可以更好地处理这个问题。
您的代码的固定版本:
package main
import "fmt"
type Name struct {
Title string
First string
Last string
}
type Location struct {
Street string
City string
State string
Zip string
}
func Merge(m interface{}) string {
switch m.(type) {
case Location:
return fmt.Sprintf("%s \n %s, %s %s", m.(Location).Street, m.(Location).City, m.(Location).State, m.(Location).Zip)
case Name:
return fmt.Sprintf("%s. %s %s", m.(Name).Title, m.(Name).First, m.(Name).Last)
}
return "Not Applicable"
}
func main() {
l := Location{
Street: "122 Broadway",
City: "New York",
State: "NY",
Zip: "1000",
}
fmt.Println(Merge(l))
}
使用fmt.String的版本:
package main
import "fmt"
type Name struct {
Title string
First string
Last string
}
func (n *Name) String() string {
return fmt.Sprintf("%s. %s %s", n.Title, n.First, n.Last)
}
type Location struct {
Street string
City string
State string
Zip string
}
func (l *Location) String() string {
return fmt.Sprintf("%s \n %s, %s %s", l.Street, l.City, l.State, l.Zip)
}
func main() {
l := &Location{
Street: "120 Broadway",
City: "New York",
State: "NY",
Zip: "1000",
}
fmt.Println(l)
n := &Name{
Title: "Mr",
First: "Billy",
Last: "Bob",
}
fmt.Println(n)
}