slice in golang

// Use of golang slice
func main() {
    // initialise the slice with 10 zero ints
	slice := make([]int, 10)
    // fillup with random ints
	fillSliceWithRand(slice)
    // prints the slice
	print(slice)
    // passes the slice by reference and reverse the slice
	reverse(slice)
    // prints the slice again
	print(slice)
}

// reverse method reverse the slice
func reverse(a []int) {
	l := len(a)
	for i := 0; i < l/2; i ++ {
		x := a[l-i-1]
		a[l-i-1] = a[i]
		a[i] = x
	}
}
// print method prints all ints of the slice
func print(a []int) {
	for _, v := range a {
		fmt.Printf("%d ", v)
	}
	fmt.Println()
}
// fillSliceWithRand method fills the slice with random ints
func fillSliceWithRand(a []int) {
	for i := 0; i < len(a); i++ {
		a[i] = rand.Intn(100)
	}
}
/**
## Slice in golang
	- Slices hold references to an underlying array, and if you 
      assign one slice to another, both refer to the same array.
	- If a function takes a slice argument, changes it makes to 
      the elements of the slice will be visible to the caller, 
      analogous to passing a pointer to the underlying array. 
**/

4.17
6
Awgiedawgie 440215 points

                                    b := [2]string{&quot;Penn&quot;, &quot;Teller&quot;}

4.17 (6 Votes)
0
4.13
8
A-312 69370 points

                                    package main

import (
	&quot;fmt&quot;
)

func main() {
	var slice = []int{1, 2, 3, 4, 5}
	var store = make([]int, 3)

	push := append(slice, 6, 7, 8, 9)
	copy(store, slice)

	fmt.Println(&quot;before append value&quot;, slice)
	fmt.Println(&quot;after append value&quot;, push)
	fmt.Println(&quot;render value all value exist&quot;, push[:])
	fmt.Println(&quot;render value after second value&quot;, push[2:])
	fmt.Println(&quot;render value before third index&quot;, push[:3])
	fmt.Println(&quot;render value start from zero and last value before second index&quot;, push[0:2])
	fmt.Println(&quot;merge slice value&quot;, store)
	fmt.Println(&quot;read size slice&quot;, len(slice))
	fmt.Println(&quot;read capacity slice&quot;, cap(slice))
}

4.13 (8 Votes)
0
4.2
10
Krish 100200 points

                                    func main() {
	s := []int{2, 3, 5, 7, 11, 13}
	printSlice(s)

	// Slice the slice to give it zero length.
	s = s[:0]
	printSlice(s)

	// Extend its length.
	s = s[:4]
	printSlice(s)

	// Drop its first two values.
	s = s[2:]
	printSlice(s)
}

func printSlice(s []int) {
	fmt.Printf(&quot;len=%d cap=%d %v\n&quot;, len(s), cap(s), s)
}

4.2 (10 Votes)
0
3.57
7
A-312 69370 points

                                    // loop over an array/a slice
for i, e := range a {
    // i is the index, e the element
}

// if you only need e:
for _, e := range a {
    // e is the element
}

// ...and if you only need the index
for i := range a {
}

// In Go pre-1.4, you'll get a compiler error if you're not using i and e.
// Go 1.4 introduced a variable-free form, so that you can do this
for range time.Tick(time.Second) {
    // do it once a sec
}

3.57 (7 Votes)
0
Are there any code examples left?
New code examples in category Go
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
&lt;&lt; in golang slice go declare slice golang slice tutorial golang slice declaration golang creating a slice go initialize slice declaration indexing in golang slice indexing in go lang slice adding to a slice golang declaring a slice in golang slice inside type golang declare slice in go lang golang create slice with length golang create slice with capacity golang find in slice golang portion of a slice golang initialzie slice golang cerate slice what is slice capacity golang golang slice type golang slice of arrays array and slice in golang 3 indexing slice golang golang slice functions how to create slice golang how to initialize slice in golang function return slice in golang how to create slice inside for golang slice length golang golang index in slice initialize slice in golang how to return slice in function of golang set slice golang go initialize slice how to create slice in golang create slice of golang slice make golang golang slice : notation golang make slice function goland slice slice golang example golang slice documentation slice in golang example golang initialize slice slice method golang golang value in slice golang literal slice slice interface golang slice from array golang slice methods golang slice define golang golang slice wkfmrl golang slice [0:] should you use slice of slice golang how to create a slice of slice golang define slice golang slice type golang golang array and slice golang array slice example slice... golang golang slice an arry how to declare a new slice in golang function that takes a slice in golang how to declare a slice in golang go what is a slice golang slice of slices golang slice javascript golang slice order golang slice length golang make slice with amount of elements how to index a slice golang golang slice strbung&uml; make a new SLice golang slice equivalent in golang golang slice include slice golang golang global slice golang slice definition golang how to extend slice golang slice subslice type of slice in golang golang slice tour why we need slice in golang slice declration in golang how to declare a slice golang slice declaration in go create a slice in go make slice of slice in golang make slice in golang define slice in golang golang slice of slice golang int slice golang slice of slcie golang make slice instance golang array slice golang return slice golang slice equivalent in other languages defineing slice in golang golang &quot;:= slice&quot; using make to create slices golang make create new slice golang what is a slice in golang get from slice golang go create slice with values slice append golang how to slice in golang slice example in golang golang slice var create slice in type golang golang copy slice golang for slice slice ... golang golang make new slice golang create slice with other slice golang create slice with values golang build slice golang new slice with values golang slice methods go initialize a slice array slice golang\ golang slice range slice of slices golang slice of a slice go modify slice golang golang slice index declare a slice in golang return slice elements golang golang interface of slice how to use slice in golang golang splice slice golang define slice golang filter slice golang index slice return slice from function golang initialize slice golang slice in golang create slice in golang make slice of slice golang golang slice o(n) slice leng golang how to declare slice of n in golang declaring a slice with values in golang what is slice in golang go gob slice go gobs slice slice of my slice golang golang create a slice slice add golang go create slice creating slice in golang golang slice example new slice golang declare slice in golang slice gol golang declare slice golang working with slice slice examples golang slice go golang array golang make slice with values golang init slice how slice work in go lang go slice array golag slice golag slice [: :] fill slice golang golang make slice initialization golang append to slice golang func accept slice slice to string golang golang slice of strings get part of slice golang create var slice in go make int array golang make a slice in golang go slice creating a slice in golang using make creating a slice in golang what does a slice do golang slice vs array golang slice methods go golang create slice of ones golang slice golang string in slice golang create list of strings declare a slice golang How to make a slice : in slice golang how to make array in golang slice golang how to define slice in golang make slice of array golang make slice golang create slice from elements golang golang make array declare a slice go lang golang slice slice create a slice in golang create slice create a slice golang golang slice make go lang create slices golang create slice make a slice golang declaring slice golang how to make a slice golang declare slice golang golang new string slice golang make list mkae an array golang (1/2)=0 in go lang go byte array make array make golang create a slice go go make slice golang array with make golang make slice golang make cap goland make array declare slice of string golang create slice golang
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source