duanmiexi2275 2015-06-16 01:59
浏览 28
已采纳

Golang在结构之间切换

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 
 %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条回答 默认 最新

  • douzuo5504 2015-06-16 03:26
    关注

    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 
     %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 
     %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)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?