Golang switch between structs

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

Golang switch between structs

问题

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

我刚开始学习golang,我正在尝试创建一个函数,根据它所用的结构体,将返回一个使用Sprintf格式化的字符串。

  1. type Name struct {
  2. Title string
  3. First string
  4. Last string
  5. }
  6. type Location struct {
  7. Street string
  8. City string
  9. State string
  10. Zip string
  11. }
  12. func Merge(m interface{}) string {
  13. switch m.(type) {
  14. case *Location:
  15. return fmt.Sprintf("%s \n %s, %s %s", m.(*Location).Street, m.(*Location).City, m.(*Location).State, m.(*Location).Zip)
  16. case *Name:
  17. return fmt.Sprintf("%s. %s %s", m.(*Name).Title, m.(*Name).First, m.(*Name).Last)
  18. }
  19. return "Not Applicable"
  20. }
  21. 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

  1. type Name struct {
  2. Title string
  3. First string
  4. Last string
  5. }
  6. type Location struct {
  7. Street string
  8. City string
  9. State string
  10. Zip string
  11. }
  12. func Merge(m interface{}) string {
  13. switch m.(type) {
  14. case *Location:
  15. return fmt.Sprintf("%s \n %s, %s %s", m.(*Location).Street, m.(*Location).City, m.(*Location).State, m.(*Location).Zip)
  16. case *Name:
  17. return fmt.Sprintf("%s. %s %s", m.(*Name).Title, m.(*Name).First, m.(*Name).Last)
  18. }
  19. return "Not Applicable"
  20. }
  21. 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接口

修复后的代码:

  1. package main
  2. import "fmt"
  3. type Name struct {
  4. Title string
  5. First string
  6. Last string
  7. }
  8. type Location struct {
  9. Street string
  10. City string
  11. State string
  12. Zip string
  13. }
  14. func Merge(m interface{}) string {
  15. switch m.(type) {
  16. case Location:
  17. return fmt.Sprintf("%s \n %s, %s %s", m.(Location).Street, m.(Location).City, m.(Location).State, m.(Location).Zip)
  18. case Name:
  19. return fmt.Sprintf("%s. %s %s", m.(Name).Title, m.(Name).First, m.(Name).Last)
  20. }
  21. return "Not Applicable"
  22. }
  23. func main() {
  24. l := Location{
  25. Street: "122 Broadway",
  26. City: "New York",
  27. State: "NY",
  28. Zip: "1000",
  29. }
  30. fmt.Println(Merge(l))
  31. }

使用fmt.String的版本:

  1. package main
  2. import "fmt"
  3. type Name struct {
  4. Title string
  5. First string
  6. Last string
  7. }
  8. func (n *Name) String() string {
  9. return fmt.Sprintf("%s. %s %s", n.Title, n.First, n.Last)
  10. }
  11. type Location struct {
  12. Street string
  13. City string
  14. State string
  15. Zip string
  16. }
  17. func (l *Location) String() string {
  18. return fmt.Sprintf("%s \n %s, %s %s", l.Street, l.City, l.State, l.Zip)
  19. }
  20. func main() {
  21. l := &Location{
  22. Street: "120 Broadway",
  23. City: "New York",
  24. State: "NY",
  25. Zip: "1000",
  26. }
  27. fmt.Println(l)
  28. n := &Name{
  29. Title: "Mr",
  30. First: "Billy",
  31. Last: "Bob",
  32. }
  33. fmt.Println(n)
  34. }
英文:

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:

  1. package main
  2. import "fmt"
  3. type Name struct {
  4. Title string
  5. First string
  6. Last string
  7. }
  8. type Location struct {
  9. Street string
  10. City string
  11. State string
  12. Zip string
  13. }
  14. func Merge(m interface{}) string {
  15. switch m.(type) {
  16. case Location:
  17. return fmt.Sprintf("%s \n %s, %s %s", m.(Location).Street, m.(Location).City, m.(Location).State, m.(Location).Zip)
  18. case Name:
  19. return fmt.Sprintf("%s. %s %s", m.(Name).Title, m.(Name).First, m.(Name).Last)
  20. }
  21. return "Not Applicable"
  22. }
  23. func main() {
  24. l := Location{
  25. Street: "122 Broadway",
  26. City: "New York",
  27. State: "NY",
  28. Zip: "1000",
  29. }
  30. fmt.Println(Merge(l))
  31. }

A version using fmt.String:

  1. package main
  2. import "fmt"
  3. type Name struct {
  4. Title string
  5. First string
  6. Last string
  7. }
  8. func (n *Name) String() string {
  9. return fmt.Sprintf("%s. %s %s", n.Title, n.First, n.Last)
  10. }
  11. type Location struct {
  12. Street string
  13. City string
  14. State string
  15. Zip string
  16. }
  17. func (l *Location) String() string {
  18. return fmt.Sprintf("%s \n %s, %s %s", l.Street, l.City, l.State, l.Zip)
  19. }
  20. func main() {
  21. l := &Location{
  22. Street: "120 Broadway",
  23. City: "New York",
  24. State: "NY",
  25. Zip: "1000",
  26. }
  27. fmt.Println(l)
  28. n := &Name{
  29. Title: "Mr",
  30. First: "Billy",
  31. Last: "Bob",
  32. }
  33. fmt.Println(n)
  34. }

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:

确定