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're mentioned
if m.Type == "message" && strings.HasPrefix(m.Text, "<@"+id+">") {
// if so try to parse if
ans := lookup(session, m.Text)
if len(ans)>0 {
// looks good, get the quote and reply with the result
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)
// NOTE: the Message object is copied, this is intentional
} else {
// huh?
m.Text = fmt.Sprintf("sorry, that does not compute\n")
postMessage(ws, m)
}
}
}
Does the loop construct just loop forever or is there an eventing system binding behind the scenes?
It is possible to create an infinite loop in Go without providing a condition in the for loop. Programmers dread the infinite loop but it can be useful when you don't want your program to close and you are waiting for something. package main import "fmt" func main() { for { fmt. Println("Infinite Loop!") } }
Try using "os. exit(0)" it will surely work.
Blocking profiler shows you the time period in which goroutines are not running (waiting). The blocking profiler might be useful if you need to find unbuffered or full channels, sync. Mutex locks, or any other bottlenecks.
If the value of the conditional statement is true, then the loop executes. The post statement is executed after the body of the for-loop. After the post statement, the condition statement evaluates again if the value of the conditional statement is false, then the loop ends.
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.
for
without any additional statements is basically the same as while (true)
in other languages, an infinite loop.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With