Discussion:
[go-nuts] slice of unaddressable value
Runix
2015-06-03 17:23:30 UTC
Permalink
Hi, why in the first slice get the error, the second is not?

var a = ([...]interface{}{})[:]
fmt.Println(a)

var b = [...]interface{}{}
fmt.Println(b[:])

http://play.golang.org/p/uxJjvNLGI5
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Vasko Zdravevski
2015-06-03 18:59:53 UTC
Permalink
Hi Runix,

The answer to your question seems to be in the spec here:
http://golang.org/ref/spec#Slice_expressions

If the sliced operand is an array, it must be addressable
<http://golang.org/ref/spec#Address_operators> and the result of the slice
operation is a slice with the same element type as the array.


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

Just needed to add an "&" to get the address of the created array. Here is
the 1 character change to your code that makes it work:
http://play.golang.org/p/M4h1dzOl7f

Hope this helps,
Vasko.
Post by Runix
Hi, why in the first slice get the error, the second is not?
var a = ([...]interface{}{})[:]
fmt.Println(a)
var b = [...]interface{}{}
fmt.Println(b[:])
http://play.golang.org/p/uxJjvNLGI5
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Runix
2015-06-03 19:19:30 UTC
Permalink
What about

As an exception to the addressability requirement, x may also be a
(possibly parenthesized) composite literal.

CompositeLit = LiteralType LiteralValue .
LiteralType = "[" "..." "]" ElementType
LiteralValue = "{" [ ElementList [ "," ] ] "}" .
Post by Vasko Zdravevski
Hi Runix,
http://golang.org/ref/spec#Slice_expressions
If the sliced operand is an array, it must be addressable
<http://golang.org/ref/spec#Address_operators> and the result of the
slice operation is a slice with the same element type as the array.
http://golang.org/ref/spec#Address_operators
Just needed to add an "&" to get the address of the created array. Here is
http://play.golang.org/p/M4h1dzOl7f
Hope this helps,
Vasko.
Post by Runix
Hi, why in the first slice get the error, the second is not?
var a = ([...]interface{}{})[:]
fmt.Println(a)
var b = [...]interface{}{}
fmt.Println(b[:])
http://play.golang.org/p/uxJjvNLGI5
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Brad Fitzpatrick
2015-06-03 19:24:36 UTC
Permalink
It needs to live somewhere in memory to take its address.

A variable is that place in memory.
Post by Runix
What about
As an exception to the addressability requirement, x may also be a
(possibly parenthesized) composite literal.
CompositeLit = LiteralType LiteralValue .
LiteralType = "[" "..." "]" ElementType
LiteralValue = "{" [ ElementList [ "," ] ] "}" .
среЎа, 3 ОюМя 2015 г., 23:59:53 UTC+5 пПльзПватель Vasko Zdravevski
Post by Vasko Zdravevski
Hi Runix,
http://golang.org/ref/spec#Slice_expressions
If the sliced operand is an array, it must be addressable
<http://golang.org/ref/spec#Address_operators> and the result of the
slice operation is a slice with the same element type as the array.
http://golang.org/ref/spec#Address_operators
Just needed to add an "&" to get the address of the created array. Here
http://play.golang.org/p/M4h1dzOl7f
Hope this helps,
Vasko.
Post by Runix
Hi, why in the first slice get the error, the second is not?
var a = ([...]interface{}{})[:]
fmt.Println(a)
var b = [...]interface{}{}
fmt.Println(b[:])
http://play.golang.org/p/uxJjvNLGI5
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Ian Lance Taylor
2015-06-03 19:26:56 UTC
Permalink
Post by Runix
What about
As an exception to the addressability requirement, x may also be a (possibly
parenthesized) composite literal.
CompositeLit = LiteralType LiteralValue .
LiteralType = "[" "..." "]" ElementType
LiteralValue = "{" [ ElementList [ "," ] ] "}" .
That appears in the section on the address operator. It is what
permits the '&' in &S{f:v}. It's not a general description of what is
addressable.

Ian
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Vasko Zdravevski
2015-06-03 19:06:38 UTC
Permalink
Hi Runix,

I trust you have a need for this, but why do you need to first create an
array literal using "[...]" and then turn that into a slice with the slice
expression? Why not just start off with a slice?

http://play.golang.org/p/8RQwhkVN6p
Post by Runix
Hi, why in the first slice get the error, the second is not?
var a = ([...]interface{}{})[:]
fmt.Println(a)
var b = [...]interface{}{}
fmt.Println(b[:])
http://play.golang.org/p/uxJjvNLGI5
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Runix
2015-06-03 19:56:21 UTC
Permalink
Thank you all for answers
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...