Golang switch语句

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

Golang switch statement

问题

我是你的中文翻译助手,以下是你提供的代码的翻译:

我刚开始学习golang,并从python转到了golang。我该如何将这个if else语句转换为"switch"语句?

type Client struct {
    Type string `json:"type"`
}

func main() {
    a := Client{"v1"}

    switch a.Type {
    case "v1":
        if err := apiClient.v1("I am version one"); err != nil {
            log.Printf(" Error: Sending type v1")
            return err
        }
    case "v2":
        if err := apiClient.v2("I am version 2"); err != nil {
            log.Printf(" Error: Sending type v2\n")
            return err
        }
    default:
        return errors.New(fmt.Sprintf(" Error: Invalid mertype...This Type"))
    }
}

希望这可以帮助到你!如果你有任何其他问题,请随时问我。

英文:

I am new to golang and switched from python to golang. How can I convert this if else statement in a "switch"

type Client struct {
    Type  string `json:"type"`
} 

Then I am initializing this struct like this:

a:= Client{"v1"}

if a.Type == "v1" {
	if err := apiClient.v1("I am version one"); err != nil {
		log.Printf(" Error: Sending type v1")
		return err
	}
} else if a.Type == "v2" {
	if err := apiClient.v2("I am version 2"); err != nil {
		log.Printf(" Error: Sending type v2\n")
		return err
	}
}  else {
		return errors.New(fmt.Sprintf(" Error: Invalid mertype...This Type"))
	}

答案1

得分: 10

即使你可以通过阅读这里的文档来自己弄清楚,以下是你应该写的内容:

switch a.Type {
case "v1":
    // ...
    // 在 Go 中不需要使用 'break'
case "v2":
    // ...
default:
    // 常规的 'else' 子句语句
}
英文:

Even though you could figure it out yourself by reading the doc here is what you should write:

switch a.Type {
case "v1":
    // ...
    // no need to 'break' in go
case "v2":
    // ...
default:
    // usual 'else' clause statements
}

答案2

得分: -1

这与其他编程语言相同,只是语法稍有不同。

你可以按照以下方式使用switch case:

switch a.Type {
case "v1":
    if err := apiClient.v1("I am version one"); err != nil {
        log.Printf(" Error: Sending type v1")
        return err
    }
case "v2":
    if err := apiClient.v1("I am version 2"); err != nil {
        log.Printf(" Error: Sending type v2")
        return err
    }
default:
    return errors.New(fmt.Sprintf(" Error: Invalid mertype...This Type"))
}

甚至可以将多个case合并为一个。

switch a.Type {
case "v1":
    if err := apiClient.v1("I am version one"); err != nil {
        log.Printf(" Error: Sending type v1")
        return err
    }
case "v2":
    if err := apiClient.v1("I am version 2"); err != nil {
        log.Printf(" Error: Sending type v2")
        return err
    }
case "v3", "v4":
    if err := apiClient.v1("I am version 3 or 4"); err != nil {
        log.Printf(" Error: Sending type v3 or v4")
        return err
    }
default:
    return errors.New(fmt.Sprintf(" Error: Invalid mertype...This Type"))
}
英文:

This is same like any other programming language, just syntax is little bit changed.

you can use switch case as follow :

switch a.Type {
case "v1":
    if err := apiClient.v1("I am version one"); err != nil {
        log.Printf(" Error: Sending type v1")
        return err
    }
case "v2":
    if err := apiClient.v1("I am version 2"); err != nil {
        log.Printf(" Error: Sending type v2")
        return err
    }
default:
    return errors.New(fmt.Sprintf(" Error: Invalid mertype...This Type"))
}

Even you can combine multiple cases into one too.
like :

switch a.Type {
case "v1":
    if err := apiClient.v1("I am version one"); err != nil {
        log.Printf(" Error: Sending type v1")
        return err
    }
case "v2":
    if err := apiClient.v1("I am version 2"); err != nil {
        log.Printf(" Error: Sending type v2")
        return err
    }
case "v3", "v4":
    if err := apiClient.v1("I am version 3 or 4"); err != nil {
        log.Printf(" Error: Sending type v3 or v4")
        return err
    }
default:
    return errors.New(fmt.Sprintf(" Error: Invalid mertype...This Type"))
}

huangapple
  • 本文由 发表于 2017年8月5日 05:15:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/45515604.html
匿名

发表评论

匿名网友

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

确定