Reassigning a slice parameter acts differently - go

package main
import "fmt"
func main() {
paths := []string{"hello", "world", "mars"}
var result = delete(paths, 1)
fmt.Println(result)
fmt.Println(paths)
}
func delete(paths []string, index int) []string {
paths = append(paths[:index], paths[index+1:]...)
return paths
}
The result of the code above is the following:
[hello mars]
[hello mars mars]
As you see, the second fmt.Println(paths) obviously uses the modified slice but does not use the reassigned value. Why is that? I was expecting it to print [hello mars] like in the print before.
I know that paths being passed is not the same slice as paths parameter in the delete() function expect for referencing the same underlying array. But I still don't understand how I changed the underlying array of the paths being passed to delete function as it prints [hello mars mars] instead of [hello world mars].

Because, as you said, the same underlying array is in use. When you do the append, paths[:1] is a slice of length 1 and capacity 3, and paths[2:] is a slice of length 1, so there is enough room in the underlying array of the first slice to append the new value without allocating a new array. paths in main is still a slice of length 3 since it was never modified, but since the underlying array was modified (specifically element 1), you see the value that you see.
You may want to take a look at https://blog.golang.org/go-slices-usage-and-internals if you haven't already.

Related

Mutate a slice passed as an argument to a function in Go

I wrote some code where a function f takes a slice s as an argument and modifies it without returning it.
Since slices can be considered as references to underlying arrays, I thought that the slice would be actually modified outside of that function's scope, but it's not the case.
An example would be the code below (https://play.golang.org/p/Y5JUmDtRXrz).
package main
import (
"fmt"
)
func pop(s []int) int {
first, s := s[0], s[1:]
return first
}
func main() {
s := []int{0, 1, 2, 3}
first := pop(s)
fmt.Println(first, s)
}
pop(s) actually returns 0, which is expected. But then in the output s still has 0 as its first element.
0 [0 1 2 3]
Program exited.
Why? And how could I solve this ?
Two separate things are happening here, both of which prevent this from behaving as you expect:
func pop(s []int) int {
first, s := s[0], s[1:]
The first (and simpler) issue is you're defining a new local variable s here, which shadows you function parameter s.
Second, slices do point to an underlying array, but the slice is still passed by copy, just like everything else. That means that:
s = s[1:]
Modifies your copy of s to have a different window on the underlying array. That doesn't change the slice in the caller. However, if you change the values in the underlying array, that will be reflected in the caller, e.g.:
s[1] = 42
You can learn more about this throughout the Tour and on the Go blog.
The line first, s := s[0], s[1:] creates a new variable s since you are using :=. On top of that if you want to modify the slice you need to pass it by pointer.
Passing it by value, it will refer to the same underlying array, but the slice itself is a copy. So changes to the underlying array would be reflected in main, but changes to the slice itself would not.
Here is an example of passing the slice by pointer.
package main
import (
"fmt"
)
func pop(s *[]int) int {
first := (*s)[0]
*s = (*s)[1:]
return first
}
func main() {
s := []int{0, 1, 2, 3}
first := pop(&s)
fmt.Println(first, s)
}

Passing arrays as function arguments

I expected the following code snippet either to produce a as {0x01, 0x02} (pass by value) or as {0x03, 0x02, 0x01} (pass by reference). Strangely, it produces the output as {0x03, 0x02}. Could you please help me understand why is that?
package main
import "fmt"
func test1(t []byte) {
t[0] = 0x03
t = append(t, 0x01 )
}
func main() {
a := []byte{0x01, 0x02 }
test1(a)
_ = a
fmt.Printf("%v", a)
}
In Go []byte is not an array, it's a byte slice.
What happened is that the size of a was 2, and because of that, after its first element was changed to 3, append allocated a new bigger slice - and t was set to the address of the new slice that was allocated, but that doesn't affect the a in main.
From A Tour of Go about append:
If the backing array of s is too small to fit all the given values a bigger array will be allocated. The returned slice will point to the newly allocated array.
The slice header is passed by value, but it references the same backing array. So when you change the first value it is changed in the original array as well. When you call append a new slice header is generated and written to the space occupied by the original argument value thus staying local to the method.

Why a slice []struct doesn't behave same as []builtin?

The slices are references to the underlying array. This makes sense and seems to work on builtin/primitive types but why is not working on structs? I assume that even if I update a struct field the reference/address is still the same.
package main
import "fmt"
type My struct {
Name string
}
func main() {
x := []int{1}
update2(x)
fmt.Println(x[0])
update(x)
fmt.Println(x[0])
my := My{Name: ""}
update3([]My{my})
// Why my[0].Name is not "many" ?
fmt.Println(my)
}
func update(x []int) {
x[0] = 999
return
}
func update2(x []int) {
x[0] = 1000
return
}
func update3(x []My) {
x[0].Name = "many"
return
}
To clarify: I'm aware that I could use pointers for both cases. I'm only intrigued why the struct is not updated (unlike the int).
What you do when calling update3 is you pass a new array, containing copies of the value, and you immediately discard the array. This is different from what you do with the primitive, as you keep the array.
There are two approaches here.
1) use an array of pointers instead of an array of values:
You could define update3 like this:
func update3(x []*My) {
x[0].Name = "many"
return
}
and call it using
update3([]*My{&my})
2) write in the array (in the same way you deal with the primitive)
arr := make([]My,1)
arr[0] = My{Name: ""}
update3(arr)
From the GO FAQ:
As in all languages in the C family, everything in Go is passed by
value. That is, a function always gets a copy of the thing being
passed, as if there were an assignment statement assigning the value
to the parameter. For instance, passing an int value to a function
makes a copy of the int, and passing a pointer value makes a copy of
the pointer, but not the data it points to. (See the next section for
a discussion of how this affects method receivers.)
Map and slice values behave like pointers: they are descriptors that
contain pointers to the underlying map or slice data. Copying a map or
slice value doesn't copy the data it points to.
Thus when you pass my you are passing a copy of your struct and the calling code won't see any changes made to that copy.
To have the function change the data in teh struct you have to pass a pointer to the struct.
Your third test is not the same as the first two. Look at this (Playground). In this case, you do not need to use pointers as you are not modifying the slice itself. You are modifying an element of the underlying array. If you wanted to modify the slice, by for instance, appending a new element, you would need to use a pointer to pass the slice by reference. Notice that I changed the prints to display the type as well as the value.

How can I call len() on an interface?

I'm writing a test that a JSON list is empty.
{"matches": []}
The object has type map[string]interface{}, and I want to test that the list is empty.
var matches := response["matches"]
if len(matches) != 0 {
t.Errorf("Non-empty match list!")
}
However I'm told at compile time that this is invalid
invalid argument matches (type interface {}) for len
If I try casting to a list type:
matches := response["matches"].([]string)
I get a panic:
panic: interface conversion: interface is []interface {}, not []string [recovered]
What do I want to write here?
JSON parsing with maps in Go uses interfaces everywhere. Imagine you have the following JSON object:
{
"stuff" : [
"stuff1",
"stuff2",
"stuff3",
]
}
The Go JSON library will parse the outer object as a map from keys to values, as you've seen in your code. It maps variable names as keys to the values that correspond to those variable names. However, since it has no way of knowing ahead of time what those values are, the value type of the map is simply interface{}. So let's say you know there's a key called "stuff", and you know that its value is an array. You could do:
arr := myMap["stuff"]
And you know that it's an array type, so you can actually instead do:
arr := myMap["stuff"].([]interface{})
the problem here is that while you're right that it's an array, and the JSON library knows this, it has no way of knowing that every element will be of type string, so there's no way for it to decide that the array type should actually be []string. Imagine if you had done this instead:
{
"stuff" : [
"stuff1",
"stuff2",
3
]
}
Well "stuff" can't now be an array of strings because one of the elements isn't a string. In fact, it can't be an array of anything - there's no single type that would satisfy the types of all of the elements. So that's why the Go JSON library has no choice but to leave it as []interface{}. Luckily, since all you want is the length, you're already done. You can just do:
arr := myMap["stuff"].([]interface{})
l := len(arr)
Now that's all fine and good, but let's say that down the road you want to actually look at one of the elements. You could now take out an element and, knowing that it's a string, do:
arr := myMap["stuff"].([]interface{})
iv := arr[0] // interface value
sv := iv.(string) // string value
NOTE
When I say "array," I mean array in the JSON sense - these are JSON arrays. The data structure that represents them in Go is called a "slice" (Go has arrays too, but they're a separate thing - if you're used to arrays in languages like C or Java, Go slices are the closest analogue).
When dealing with JSON, you can add type declarations for array and object, then add methods as needed to help with conversion:
package main
import "encoding/json"
type (
array []interface{}
object map[string]interface{}
)
func (o object) a(s string) array {
return o[s].([]interface{})
}
func main() {
data := []byte(`{"matches": []}`)
var response object
json.Unmarshal(data, &response)
matches := response.a("matches")
mLen := len(matches)
println(mLen == 0)
}
https://golang.org/ref/spec#Type_declarations

Slice storage reference in Go

In the Go library source you often see that a slice is passed by creating a new slice storage reference like so
method(s[:])
What's the benefit of this, compared to just passing the original slice?
method(s)
The s[:] construct is normally used only to create a new slice referencing an existing array, not for "passing the original slice".
If s[:] is really used somewhere in the stdlib and s is a slice than it could be e.g. a refactoring leftover. Please report such place if known to you on the Go issue tracker.
The only case where you would see code like this is when s is an array, and you want to pass as a parameter to a function that takes a slice as its input. Take the following code.
package main
func main() {
x := [...]int{1, 2, 3, 4, 5}
someFunction(x) // type mismatch error : expecting [] int, passed [5] int
someFunction(x[:])// no error
}
func someFunction(input []int){
// use input
}
The thing to note here is that [] int and [5] int are entirely different types.

Resources