You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
What steps will reproduce the problem?
Compile (8g), link (8l), and run this program:
package main
import (
"fmt";
"strconv"
)
func main() {
var s1, s2 string;
s1 = "a"+f1()+"b"+f1()+"c"+f1();
fmt.Println(s1);
s2 = "a" + f1();
s2 += "b" + f1();
s2 += "c" + f1();
fmt.Println(s2);
}
var count int = 0;
func f1() string {
count++;
fmt.Println("times f1() called =", count);
return strconv.Itoa(count);
}
What is the expected output? What do you see instead?
Should be:
times f1() called = 1
times f1() called = 2
times f1() called = 3
a1b2c3
times f1() called = 4
times f1() called = 5
times f1() called = 6
a4b5c6
Get:
times f1() called = 1
times f1() called = 2
times f1() called = 3
a3b2c1
times f1() called = 4
times f1() called = 5
times f1() called = 6
a4b5c6
Looking at the assembly output, you can see that the function, f1(), is
called left-to-right three times and the results pushed on the stack. Then
the results are popped off the stack and concatenated to form string s1,
making it look like f1() was called right-to-left.
What is your $GOOS? $GOARCH?
linux, 386
Which revision are you using? (hg identify)
47a7d31e4405+ tip
Please provide any additional information below.
This example program is based on the discussion on the golang-nuts group
called "String concatenation evaluates Right to Left, not Left to Right."
started by ray (r...@hollett.demon.co.uk) Dec. 1st.
The text was updated successfully, but these errors were encountered:
Parenthesizing the left-hand side does make a difference [for example, s1 =
(("a"+f1())+"b"+f1())+"c"+f1();]. I found no reference to sequence points in the
language specification. Perhaps go does not have them, the way C does.
The text was updated successfully, but these errors were encountered: