在Go语言中,无法在函数内部定义变量。

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

cannot define variable in function - Go

问题

我对这个看似简单的任务遇到了一些麻烦。

我有一个函数,它将一个包含小时和分钟值的结构体格式化为字符串。

  1. type Clock struct {
  2. h int
  3. m int
  4. }
  5. func (c *Clock) String() string {
  6. h string
  7. m string
  8. if c.m < 10 {
  9. m := fmt.Sprintf("0%d", c.m)
  10. } else {
  11. m := fmt.Sprintf("%d", c.m)
  12. }
  13. if c.h < 10 {
  14. h := fmt.Sprintf("0%d", c.h)
  15. } else {
  16. h := fmt.Sprintf("%d", c.h)
  17. }
  18. return fmt.Sprintf("%s:%s", h, m)
  19. }

我得到的错误是:

语法错误:意外的名称,期望分号或换行符或 },出现在上面的 h string 这一行。

你知道这里出了什么问题吗?我原以为我只需要使用一个临时变量来格式化整数值。

英文:

I am having a little trouble with what I though was a simple task.

I have a function that formats a struct that holds an hour and a minute value and formats it into a string.

  1. type Clock struct {
  2. h int
  3. m int
  4. }
  5. func (c *Clock) String() string {
  6. h string
  7. m string
  8. if c.m &lt; 10 {
  9. m := fmt.Sprintf(&quot;0%d&quot;, c.m)
  10. } else {
  11. m := fmt.Sprintf(&quot;%d&quot;, c.m)
  12. }
  13. if c.h &lt; 10 {
  14. h := fmt.Sprintf(&quot;0%d&quot;, c.h)
  15. } else {
  16. h := fmt.Sprintf(&quot;%d&quot;, c.h)
  17. }
  18. return fmt.Sprintf(&quot;%s:%s&quot;, h, m)
  19. }

The error I am getting is:

syntax error: unexpected name, expecting semicolon or newline or } for the line h string above.

Any idea what is going on here? I figured I would simple use a temporary variable to format the int values

答案1

得分: 5

你需要声明:

  1. var h string
  2. var m string

在给 hm 赋值时,不要使用 :=,而是使用 =(否则你只会在它们的内部作用域中定义变量,而不是重用之前定义的变量)

  1. if c.m < 10 {
  2. m = fmt.Sprintf("0%d", c.m)
  3. } else {
  4. m = fmt.Sprintf("%d", c.m)
  5. }
  6. if c.h < 10 {
  7. h = fmt.Sprintf("0%d", c.h)
  8. } else {
  9. h = fmt.Sprintf("%d", c.h)
  10. }

完整示例:play.golang.org

输出结果:Hello, playground 02:08

英文:

You need to declare:

  1. var h string
  2. var m string

And do not use := but = when affecting values to h and m (or you would define those only in their inner scope, instead of reusing the variables defined before)

  1. if c.m &lt; 10 {
  2. m = fmt.Sprintf(&quot;0%d&quot;, c.m)
  3. } else {
  4. m = fmt.Sprintf(&quot;%d&quot;, c.m)
  5. }
  6. if c.h &lt; 10 {
  7. h = fmt.Sprintf(&quot;0%d&quot;, c.h)
  8. } else {
  9. h = fmt.Sprintf(&quot;%d&quot;, c.h)
  10. }

Full example: play.golang.org

Output: Hello, playground 02:08

答案2

得分: 0

声明String方法的变量时,只需使用var关键字进行一次声明,不要使用短变量声明(:=)重新声明它们。例如,

  1. package main
  2. import "fmt"
  3. type Clock struct {
  4. h int
  5. m int
  6. }
  7. func (c *Clock) String() string {
  8. var (
  9. h string
  10. m string
  11. )
  12. if c.m < 10 {
  13. m = fmt.Sprintf("0%d", c.m)
  14. } else {
  15. m = fmt.Sprintf("%d", c.m)
  16. }
  17. if c.h < 10 {
  18. h = fmt.Sprintf("0%d", c.h)
  19. } else {
  20. h = fmt.Sprintf("%d", c.h)
  21. }
  22. return fmt.Sprintf("%s:%s", h, m)
  23. }
  24. func main() {}

参考资料:

Go编程语言规范

声明和作用域

类型声明

变量声明

短变量声明

英文:

Declare the String method variables once (var). Do not redeclare them with short variable declarations (:=). For example,

  1. package main
  2. import &quot;fmt&quot;
  3. type Clock struct {
  4. h int
  5. m int
  6. }
  7. func (c *Clock) String() string {
  8. var (
  9. h string
  10. m string
  11. )
  12. if c.m &lt; 10 {
  13. m = fmt.Sprintf(&quot;0%d&quot;, c.m)
  14. } else {
  15. m = fmt.Sprintf(&quot;%d&quot;, c.m)
  16. }
  17. if c.h &lt; 10 {
  18. h = fmt.Sprintf(&quot;0%d&quot;, c.h)
  19. } else {
  20. h = fmt.Sprintf(&quot;%d&quot;, c.h)
  21. }
  22. return fmt.Sprintf(&quot;%s:%s&quot;, h, m)
  23. }
  24. func main() {}

References:

The Go Programming Language Specification

Declarations and scope

Type declarations

Variable declarations

Short variable declarations

huangapple
  • 本文由 发表于 2014年9月25日 04:22:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/26025805.html
匿名

发表评论

匿名网友

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

确定