Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to append to a slice pointer receiver

Tags:

I have a type alias for a slice. And I want to be able to append to the slice (or filter from the slice) when the slice is a pointer receiver:

package main  import (     "fmt" )  type itself []string  func (h itself) appendToItself(test string) {     h = append(h, test) }  func main() {     h := itself{"1", "2"}     h.appendToItself("3")     fmt.Println(h, "<- how do I make it [1,2,3]") } 

Log:

[1 2] <- how do I make it [1,2,3] 
like image 505
samol Avatar asked Apr 26 '16 02:04

samol


People also ask

Does append return a new slice?

The append built-in function appends elements to the end of a slice. If it has sufficient capacity, the destination is resliced to accommodate the new elements. If it does not, a new underlying array will be allocated. Append returns the updated slice.

Is slice passed by reference?

the memory block containing the slice elements is passed by "reference". The slice information triplet holding the capacity, the number of element and the pointer to the elements is passed by value.

Are slices in Go pointers?

Slices are pointers to arrays, with the length of the segment, and its capacity. They behave as pointers, and assigning their value to another slice, will assign the memory address.

Is slice reference type Golang?

map, pointer and slice types are reference types in Go. map, pointer, slice, channel, function and interface types are reference types.

How to append a slice in JavaScript?

The built-in append function appends any number of elements to the end of a slice. Append returns the updated slice and it does not modify the existing one. So it is Pure function in terms of Javascript. The resulting value of append () function is a slice containing all the elements of the original slice plus the provided values.

Are slices passed by reference or pointers?

It is a common belief that slices are passed by reference, in fact, the following example will print [b,b] and [b,b] even if the slice was initialized to [a,a] since it got modified during the execution of the literal function and the change is visible to the main. Making use of pointers leads to the same result, in fact, the following code

Why do we need a pointer to a slice in Python?

The answer to the questions, then, is simple, but hidden inside the implementation of the slice itself: The pointer to a slice is indispensable when the function is going to modify the structure, the size, or the location in memory of the slice and every change should to be visible to those who call the function.

Which function appends the new element at the end of slice?

This function appends the new element at the end of the slice. Here, this function takes s slice and x…T means this function takes a variable number of arguments for the x parameter. Such type of function is also known as a variadic function.


1 Answers

You need to actually pass a pointer, try:

package main  import (     "fmt" )  type itself []string  func (h *itself) appendToItself(test string) {     *h = append(*h, test) }  func main() {     h := itself{"1", "2"}     h.appendToItself("3")     fmt.Println(h, "<- how do I make it [1,2,3]") } 
like image 167
Akavall Avatar answered Oct 18 '22 15:10

Akavall