一个没有条件的 golang 的 { ..block.. } 是什么意思?

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

What does a golang for { ..block.. } without conditions do?

问题

我是一个golang新手,我遇到了一个非常有趣的控制结构,它不遵循经典的命令式for循环结构。我无法在文档中找到关于这个结构的信息。以下是相关的代码:

for {
  // 读取每个传入的消息
  m, err := getMessage(ws)
  if err != nil {
    log.Fatal(err)
  }   

  // 检查是否提到了我们
  if m.Type == "message" && strings.HasPrefix(m.Text, "<@"+id+">") {
    // 如果是,尝试解析它
    ans := lookup(session, m.Text)
    if len(ans)>0 {
      // 看起来不错,获取引用并回复结果
      go func(m Message) {
        for _, def := range ans {
          if len(def[1]) > 0 { 
            m.Text = "*" + def[0] + " " + def[1] + "*: " + def[2]
          } else {
            m.Text = "*" + def[0] + "*: " + def[2]
          }   
          postMessage(ws, m)
        }   
      }(m)
      // 注意:Message对象被复制,这是有意的
    } else {
      // 嗯?
      m.Text = fmt.Sprintf("抱歉,无法计算\n")
      postMessage(ws, m)
    }   
  }   
}

这个循环结构会一直循环下去,除非有外部事件触发退出。

英文:

I'm a golang neophyte and I've come across a rather interesting control structure which doesn't follow the classical imperative for-loop construct. I've been unable to locate on documentation on the structure either. The following is the code in question:

  for {
    // read each incoming message
    m, err := getMessage(ws)
    if err != nil {
      log.Fatal(err)
    }   
 
    // see if we&#39;re mentioned
    if m.Type == &quot;message&quot; &amp;&amp; strings.HasPrefix(m.Text, &quot;&lt;@&quot;+id+&quot;&gt;&quot;) {
      // if so try to parse if
      ans := lookup(session, m.Text)
      if len(ans)&gt;0 {
        // looks good, get the quote and reply with the result
        go func(m Message) {
          for _, def := range ans {
            if len(def[1]) &gt; 0 { 
              m.Text = &quot;*&quot; + def[0] + &quot; &quot; + def[1] + &quot;*: &quot; + def[2]
            } else {
              m.Text = &quot;*&quot; + def[0] + &quot;*: &quot; + def[2]
            }   
            postMessage(ws, m)
            }   
        }(m)
        // NOTE: the Message object is copied, this is intentional
      } else {
        // huh?
        m.Text = fmt.Sprintf(&quot;sorry, that does not compute\n&quot;)
        postMessage(ws, m)
      }   
    }   
  }

Does the loop construct just loop forever or is there an eventing system binding behind the scenes?

答案1

得分: 24

《Go编程语言规范》

对于语句

一个“for”语句指定了一个块的重复执行。迭代由条件、“for”子句或“range”子句控制。

ForStmt = "for" [ Condition | ForClause | RangeClause ] Block .
Condition = Expression .

在其最简单的形式中,一个“for”语句指定了一个块的重复执行,只要布尔条件求值为true。条件在每次迭代之前进行求值。如果条件不存在,则等同于布尔值true。

如果条件不存在,例如for { ... },它等同于布尔值true,例如for true { ... }。有时它被称为无限循环。因此,您将需要另一种机制,如breakreturn,来终止循环。

关于“for”语句的文档可以在《Go编程语言规范》中找到。

英文:

> The Go Programming Language Specification
>
> For statements
>
> A "for" statement specifies repeated execution of a block. The
> iteration is controlled by a condition, a "for" clause, or a "range"
> clause.
>
> ForStmt = "for" [ Condition | ForClause | RangeClause ] Block .
> Condition = Expression .
>
> In its simplest form, a "for" statement specifies the repeated
> execution of a block as long as a boolean condition evaluates to true.
> The condition is evaluated before each iteration. If the condition is
> absent, it is equivalent to the boolean value true.

If the condition is absent, for example, for { ... }, it is equivalent to the boolean value true, for example for true { ... }. It is sometimes referred to as an infinite loop. Therefore, you will need another mechanism, such as break or return, to terminate the loop.

The documentation for the for statement is the The Go Programming Language Specification.

答案2

得分: 12

for在其他语言中,如果没有其他附加语句,基本上与while (true)是一样的,都是一个无限循环。

英文:

for without any additional statements is basically the same as while (true) in other languages, an infinite loop.

huangapple
  • 本文由 发表于 2016年1月22日 01:00:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/34929803.html
匿名

发表评论

匿名网友

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

确定