FAQ
Using a multiple assignment into a slice:

package main

import "fmt"

func main() {
s := []int{1, 2, 3, 4, 5}
_len := len(s)
s[0], s[_len-1] = s[_len-1], s[0] // s[0] = 5; s[4] = s[0]
fmt.Println(s)
}

[5 2 3 4 1]

After of assign 5 to s[0], I was expecting that the second assignment were
to take that value but it takes the original value. So, all assignments are
done from the original slice, right?

--

Search Discussions

  • Job van der Zwan at Nov 4, 2012 at 10:37 am

    On Sunday, 4 November 2012 11:26:11 UTC+1, Archos wrote:

    Using a multiple assignment into a slice:

    package main

    import "fmt"

    func main() {
    s := []int{1, 2, 3, 4, 5}
    _len := len(s)
    s[0], s[_len-1] = s[_len-1], s[0] // s[0] = 5; s[4] = s[0]
    fmt.Println(s)
    }

    [5 2 3 4 1]

    After of assign 5 to s[0], I was expecting that the second assignment were
    to take that value but it takes the original value. So, all assignments are
    done from the original slice, right?
    Does this answer your question? :)

    http://golang.org/ref/spec#Assignments

    The assignment proceeds in two phases. First, the operands of index
    expressions <http://golang.org/ref/spec#Indexes> and pointer indirections<http://golang.org/ref/spec#Address_operators> (including
    implicit pointer indirections in selectors<http://golang.org/ref/spec#Selectors>)
    on the left and the expressions on the right are all evaluated in the
    usual order <http://golang.org/ref/spec#Order_of_evaluation>. Second, the
    assignments are carried out in left-to-right order.
    --
  • Archos at Nov 4, 2012 at 10:41 am
    Yes, although I came to the conclussion after of this simple assignment:

    package main

    import "fmt"

    func main() {
    a := 1
    b := 9
    a, b = b, a
    fmt.Println(a, b)
    }

    which is very good in Go but bad for me at the time of translating to JS.

    El domingo, 4 de noviembre de 2012 10:37:03 UTC, Job van der Zwan escribió:
    On Sunday, 4 November 2012 11:26:11 UTC+1, Archos wrote:

    Using a multiple assignment into a slice:

    package main

    import "fmt"

    func main() {
    s := []int{1, 2, 3, 4, 5}
    _len := len(s)
    s[0], s[_len-1] = s[_len-1], s[0] // s[0] = 5; s[4] = s[0]
    fmt.Println(s)
    }

    [5 2 3 4 1]

    After of assign 5 to s[0], I was expecting that the second assignment
    were to take that value but it takes the original value. So, all
    assignments are done from the original slice, right?
    Does this answer your question? :)

    http://golang.org/ref/spec#Assignments

    The assignment proceeds in two phases. First, the operands of index
    expressions <http://golang.org/ref/spec#Indexes> and pointer indirections<http://golang.org/ref/spec#Address_operators> (including
    implicit pointer indirections in selectors<http://golang.org/ref/spec#Selectors>)
    on the left and the expressions on the right are all evaluated in the
    usual order <http://golang.org/ref/spec#Order_of_evaluation>. Second,
    the assignments are carried out in left-to-right order.
    --

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedNov 4, '12 at 10:26a
activeNov 4, '12 at 10:41a
posts3
users2
websitegolang.org

2 users in discussion

Archos: 2 posts Job van der Zwan: 1 post

People

Translate

site design / logo © 2022 Grokbase