Arrays
In Go, anarray
is a numbered sequence of elements of a single type with a fixed length(You can not resizearrays
in Go). There are several ways to declare anarray
in Go. Here are some examples.
①varname[num]Type②varname[num]Type=[num]Type{ele1,ele2,elen....}③varname=[...]Type{ele1,ele2,elen...}//name: The name of the array.//num: The number of elements that array can contain.//Type: The type of elements that the array contains.
In the following code, I'll demonstrate how to declare anarr
which is composed of 3 strings in the three ways that I presented above.
① var name[num]Type
packagemainimport"fmt"funcmain(){vararr[3]stringarr[0]="Go"arr[1]="Ruby"arr[2]="Javascript"fmt.Println(arr)//=> [Go Ruby Javascript]}
② var name[num]Type = [num]Type { ele1, ele2, elen.... }
packagemainimport"fmt"funcmain(){vararr[3]string=[3]string{"Go","Ruby","Javascript"}fmt.Println(arr)//=> [Go Ruby Javascript]}
③ var name = [...] Type { ele1, ele2, elen... }
packagemainimport"fmt"funcmain(){arr3:=[...]string{"Go","Ruby","Javascript"}fmt.Println(arr3)//=> [Go Ruby Javascript]}
Slices
In Go, Aslice
is a segment of an array.Slices
build onarrays
and provide more power, flexibility, and convenience compared toarrays
. Just likearrays
,Slices
are indexable and have a length. But unlikearrays
, they can be resized.
Declaring a Slice
ASlice
can be declared in the following ways. Unlike declaring anarray
, you don't have to specify the number of elements theslice
can contain.
①varname[]Type②varname[]Type=[]Type{ele1,ele2,elen....}③name:=Array[start:end]④name:=make([]Type,len,cap)//name: The name of the array.//Type: The type of elements that the array contains.//Array: An array.//make: The built-in make function.
In the following code, I'll demonstrate how to declare aslice
using the four different ways of defining aslice
I presented above.
① var name[]Type
packagemainimport"fmt"funcmain(){varslice1[]stringvarslice2=[]int{1,2,3}fmt.Println(slice1)//=> []fmt.Println(slice2)//=> [1 2 3]}
② var name[]Type = []Type { ele1, ele2, elen.... }
packagemainimport"fmt"funcmain(){varslice2[]string=[]string{"Go","Ruby"}fmt.Println(slice2)//=> [Go Ruby]}
③ name := Array[start:end]
As I stated above, Aslice
is a segment of anarray
. That means we can create a slice from an array. The table below explains how to control the elements aslice
which is created from an array calledArray
.
操作 | 意味 |
---|---|
Array[low:high] | Fromlow tohigh - 1 . |
Array[low:] | Fromlow to the last element. |
Array[:high] | From the first elemet tohigh - 1 . |
Array[:] | From the first element to the last element. |
packagemainimport"fmt"funcmain(){arry:=[6]int{1,2,3,4,5,6}slice3:=arry[0:2]slice4:=arry[0:]slice5:=arry[:4]slice6:=arry[:]fmt.Println(slice3)//=> [1 2]fmt.Println(slice4)//=> [1 2 3 4 5 6]fmt.Println(slice5)//=> [1 2 3 4]fmt.Println(slice6)//=> [1 2 3 4 5 6]}
④ name := make( []Type, len, cap)
packagemainimport"fmt"funcmain(){slice7:=make([]string,2,2)fmt.Println(slice7)//=> [ ]}
Modifying a slice
As I mentioned several times, aslice
is a segment of anarray
and it refers to an underlying array. Therefore, modifying the elements of aslice
will also modify the corresponding elements in the referenced array.
packagemainimport"fmt"funcmain(){arry:=[...]string{"Go","Ruby","Javascript"}slice:=arry[:]fmt.Println(slice)//=> [Go Ruby Javacript]slice[0]="Python"fmt.Println(slice)//=> [Python Ruby Javascript]fmt.Println(arry)//=> [Python Ruby Javascript]}
Length and Capacity of a Slice
Aslice
has both alength
and acapacity
.
- The length of a slice is the number of elements it contains.
- The capacity of a slice is the number of elements in the underlying array, counting from the first element in the slice.
You can find the length and capacity of a slice using the built-in functionslen()
andcap()
.
packagemainimport"fmt"funcmain(){arry:=[...]string{"Go","Ruby","Javascript"}slice:=arry[:1]fmt.Println(len(slice),cap(slice))//=> 1 3}
Appending to a slice
It is common to append new elements to a slice, and so Go provides a built-inappend
function. It takes a slice and new elements that you want to append to theslice
. It then returns a new slice containing all the elements from the given slice as well as the new elements.
The following code shows the structure ofappend
function.
funcappend(slice[]Type,new_elements)[]Type//Type: The type of elements that the array contains.
packagemainimport"fmt"funcmain(){arry:=[...]string{"Go","Ruby","Javascript"}slice:=arry[:]varnew_slice=append(slice,"Java","Swift","C")fmt.Println(new_slice)//=> [[Go Ruby Javascript Java Swift C]}
Nil slices
The zero value of a slice isnil
. Anil slice
has alength
andcapacity
of 0 and has no underlying array.
packagemainimport"fmt"funcmain(){varnil_slice[]intifnil_slice==nil{fmt.Println("nil")//=> nil}
Top comments(2)
For further actions, you may consider blocking this person and/orreporting abuse