Golang switch between structs

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

Golang switch between structs

问题

我是你的中文翻译助手,以下是你要翻译的内容:

我刚开始学习golang,我正在尝试创建一个函数,根据它所用的结构体,将返回一个使用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"。

英文:

I'm new to golang and I'm trying to create a function that, based on the struct it's used on, will return a formatted string using 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))

I'm getting the "Not Applicable" message from my PrintLn. In one version of the code, I believe the message was "out of index".

答案1

得分: 9

在你的示例中,你试图将结构体本身传递给函数,而不是结构体的实例。当我运行你的代码时,它无法编译。不过,我同意上面的评论,通过确保每个结构体满足fmt.Stringer接口来处理会更好。

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)
}
英文:

In your example you are trying to pass the struct itself to the function rather than an instance of the struct. When I ran your code it wouldn't compile. I agree with the comments above though, this would be much better handled by making sure each struct satisfies the fmt.Stringer interface.

fmt.Stringer interface

A fixed version of your code:

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))
}

A version using 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)
}

huangapple
  • 本文由 发表于 2015年6月16日 09:59:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/30857687.html
匿名

发表评论

匿名网友

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

确定