Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

String concatenation can appear to be right-to-left. #370

Closed
cldorian opened this issue Dec 3, 2009 · 4 comments
Closed

String concatenation can appear to be right-to-left. #370

cldorian opened this issue Dec 3, 2009 · 4 comments

Comments

@cldorian
Copy link
Contributor

cldorian commented Dec 3, 2009

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.
@cldorian
Copy link
Contributor Author

cldorian commented Dec 3, 2009

Comment 1:

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.

@rsc
Copy link
Contributor

rsc commented Dec 3, 2009

Comment 2:

Owner changed to r...@golang.org.

Status changed to Started.

@rsc
Copy link
Contributor

rsc commented Dec 3, 2009

Comment 3:

Labels changed: added compilerbug.

@rsc
Copy link
Contributor

rsc commented Dec 3, 2009

Comment 4:

This issue was closed by revision 272d156.

Status changed to Fixed.

Merged into issue #-.

@golang golang locked and limited conversation to collaborators Jun 24, 2016
@rsc rsc removed their assignment Jun 22, 2022
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants