Giter Club home page Giter Club logo

7_days_of_go's Introduction

7_days_of_go's People

Stargazers

Roman Karpov avatar Aizekjon avatar  avatar HnQ avatar Vagner avatar Yusuf Yakubov avatar Aleksandr Lukashkin avatar Vlad Coman avatar Rayner Toh avatar  avatar Novojit Das avatar  avatar Alberto Alejandro Noste Pérez avatar Aleksander Petrushkin avatar Erol Sarp Konaklioglu avatar Vladimir Ponomarev avatar Fashad Ahmed avatar Johnny Squardo avatar Ebi avatar astrolemonade avatar 小野 avatar hecartic avatar  avatar Nikunj Shukla avatar Nik Sytnik avatar Rishikesh Vishwakarma avatar MiniApple avatar Alexander Bosy avatar Thien Chi Vi avatar Theofanis Despoudis avatar  avatar Júlio Miguel avatar Mac Kwan avatar Triet Minh avatar Quésia Santos avatar Alireza Naghdipour avatar AdrianSK avatar Kan Ouivirach avatar  avatar Justin Bealer avatar Rajmohan R avatar Momoh Philip J. avatar Craig avatar NoahZhang avatar Furkan Doğan avatar Sooraj Bharadwaj avatar Marco Aurélio avatar Ivan Scherbakov avatar Satoru Mikami avatar Prafull Kotecha avatar latermonk avatar Cyril Jovet avatar Dmitri Telinov avatar Hadi avatar Andrei Kostiuchenko avatar Candido Sales Gomes avatar Sureerat Kaewkeeree avatar Olivier avatar kanisorn siripatkerdpong avatar Fatih avatar  avatar Vangelis KRITSIMAS avatar  avatar Hugo de Castro avatar yyyanghj avatar RaisCui avatar  avatar  avatar Vasilii avatar  avatar Gustave Anthony Zoumedor avatar  avatar  avatar Tinnaphat Somsang avatar l2D avatar Desmond Batz avatar enixlee avatar Alexandre Suriano avatar  avatar Thanathip Suwannakhot avatar Kun Liu avatar maiyang avatar doemsche avatar Oscar Tisnado avatar Chanda Mulenga avatar Michael Rödel avatar Thai Nguyen Hung avatar Pongpanot Chuaysakun avatar Mohamed Qassem avatar Bharat Rajora avatar Vincent Van der Kussen avatar  avatar MidSeeLee avatar Bikram Sarkar avatar Juk - a search builder avatar Supakorn I. avatar Miqueas avatar James D. avatar Manassarn "Noom" Manoonchai avatar Sipatha Shoko avatar

7_days_of_go's Issues

Code explanation: slices

package main

import "fmt"

func main() {
	// 1. basic slice
	a := []int{1, 2, 3, 5, 6, 7, 8, 9, 10}
	a = a[:7]
	fmt.Println(a) // [1 2 3 5 6 7 8]

	// 2. slices are like references to arrays
	pets := []string{"cat", "dog", "bird", "fish"}
	fmt.Println(pets) // [cat dog bird fish]
	list := pets[0:2]
	fmt.Println(list) // [cat dog]
	list[1] = "pig"
	fmt.Println(list) // [cat pig]
	fmt.Println(pets) // [cat pig bird fish]

	// 3. slice length & capacity
	// The capacity of a slice is the number of elements in the underlying array, counting from the first element in the slice.
	s := []int{2, 3, 5, 7, 11, 13}
	fmt.Printf("len=%d cap=%d %v\n", len(s), cap(s), s)
	// len=6 cap=6 [2 3 5 7 11 13]

	// 4. slice the slice to give it zero length.
	s = s[:1]
	fmt.Printf("len=%d cap=%d %v\n", len(s), cap(s), s)
	// len=1 cap=6 [2]

	// Extend its length.
	s = s[:2]
	fmt.Printf("len=%d cap=%d %v\n", len(s), cap(s), s)
	// len=2 cap=6 [2 3]

	// Drop its first two values.
	s = s[2:]
	fmt.Printf("len=%d cap=%d %v\n", len(s), cap(s), s)
	// len=0 cap=4 []

	s = s[:4]
	fmt.Printf("len=%d cap=%d %v\n", len(s), cap(s), s)
	// len=4 cap=4 [5 7 11 13]

	// 5. nil slices
	// A nil slice has a length and capacity of 0 and has no underlying array.
	var t []int
	fmt.Println(t, len(t), cap(t)) // [] 0 0
	if t == nil {
		fmt.Println("nil!") // nil!
	}

	// 6. Creating a slice with make
	x := make([]int, 10)
	fmt.Printf("%d %d %v \n", len(x), cap(x), x)
	// 10 10 [0 0 0 0 0 0 0 0 0 0]

	y := make([]int, 0, 5)
	fmt.Printf("%d %d %v \n", len(y), cap(y), y)
	// 0 5 []

	z := y[:5]
	fmt.Printf("%d %d %v \n", len(z), cap(z), z)
	// 5 5 [0 0 0 0 0]

	// 7. Appending to a slice
	// cap will auto scale up depend on the added elements
	m := []int{}
	fmt.Printf("len=%d cap=%d %v\n", len(m), cap(m), m)
	// len=0 cap=0 []

	m = append(m, 6, 5)
	fmt.Printf("len=%d cap=%d %v\n", len(m), cap(m), m)
	// len=2 cap=2 [6 5]

	m = append(m, 1, 2, 5, 6, 8, 9, 3)
	fmt.Printf("len=%d cap=%d %v\n", len(m), cap(m), m)
	// len=9 cap=10 [6 5 1 2 5 6 8 9 3]

	// 8. range
	// contain i(index) & v(clone value)
	var numbers []int = []int{1, 3, 7, 8}
	for i, v := range numbers {
		fmt.Println(i, v)
	}
	// 0 1
	// 1 3
	// 2 7
	// 3 8

	// skip the index or value by assigning to _
}
package main

import "fmt"

func main() {
	// 1. basic slice
	a := []int{1, 2, 3, 5, 6, 7, 8, 9, 10}
	a = a[:7]
	fmt.Println(a) // [1 2 3 5 6 7 8]

	// 2. slices are like references to arrays
	pets := []string{"cat", "dog", "bird", "fish"}
	fmt.Println(pets) // [cat dog bird fish]
	list := pets[0:2]
	fmt.Println(list) // [cat dog]
	list[1] = "pig"
	fmt.Println(list) // [cat pig]
	fmt.Println(pets) // [cat pig bird fish]

	// 3. slice length & capacity
	// The capacity of a slice is the number of elements in the underlying array, counting from the first element in the slice.
	s := []int{2, 3, 5, 7, 11, 13}
	fmt.Printf("len=%d cap=%d %v\n", len(s), cap(s), s)
	// len=6 cap=6 [2 3 5 7 11 13]

	// 4. slice the slice to give it zero length.
	s = s[:1]
	fmt.Printf("len=%d cap=%d %v\n", len(s), cap(s), s)
	// len=1 cap=6 [2]

	// Extend its length.
	s = s[:2]
	fmt.Printf("len=%d cap=%d %v\n", len(s), cap(s), s)
	// len=2 cap=6 [2 3]

	// Drop its first two values.
	s = s[2:]
	fmt.Printf("len=%d cap=%d %v\n", len(s), cap(s), s)
	// len=0 cap=4 []

	s = s[:4]
	fmt.Printf("len=%d cap=%d %v\n", len(s), cap(s), s)
	// len=4 cap=4 [5 7 11 13]

	// 5. nil slices
	// A nil slice has a length and capacity of 0 and has no underlying array.
	var t []int
	fmt.Println(t, len(t), cap(t)) // [] 0 0
	if t == nil {
		fmt.Println("nil!") // nil!
	}

	// 6. Creating a slice with make
	x := make([]int, 10)
	fmt.Printf("%d %d %v \n", len(x), cap(x), x)
	// 10 10 [0 0 0 0 0 0 0 0 0 0]

	y := make([]int, 0, 5)
	fmt.Printf("%d %d %v \n", len(y), cap(y), y)
	// 0 5 []

	z := y[:5]
	fmt.Printf("%d %d %v \n", len(z), cap(z), z)
	// 5 5 [0 0 0 0 0]

	// 7. Appending to a slice
	// cap will auto scale up depend on the added elements
	m := []int{}
	fmt.Printf("len=%d cap=%d %v\n", len(m), cap(m), m)
	// len=0 cap=0 []

	m = append(m, 6, 5)
	fmt.Printf("len=%d cap=%d %v\n", len(m), cap(m), m)
	// len=2 cap=2 [6 5]

	m = append(m, 1, 2, 5, 6, 8, 9, 3)
	fmt.Printf("len=%d cap=%d %v\n", len(m), cap(m), m)
	// len=9 cap=10 [6 5 1 2 5 6 8 9 3]

	// 8. range
	// contain i(index) & v(clone value)
	var numbers []int = []int{1, 3, 7, 8}
	for i, v := range numbers {
		fmt.Println(i, v)
	}
	// 0 1
	// 1 3
	// 2 7
	// 3 8

	// skip the index or value by assigning to _
}

go mod

go mod tidy
go mod vendor
go build -mod=vendor

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.