Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Go Arrays & Slices
Search
Nathan Youngman
May 25, 2015
Programming
0
100
Go Arrays & Slices
An overview of how slices and arrays work in Go.
Nathan Youngman
May 25, 2015
Tweet
Share
More Decks by Nathan Youngman
See All by Nathan Youngman
The Healthy Programmer
nathany
2
91
Go and Node.js: a comparison
nathany
1
150
Diet Hacks
nathany
2
330
Go 1.6 and HTTP/2
nathany
3
92
Upgrading Rails Redux
nathany
1
78
GopherCon recap
nathany
0
130
Go Functions
nathany
0
74
Go Types
nathany
2
110
Go Packages
nathany
2
410
Other Decks in Programming
See All in Programming
From Spring Boot 2 to Spring Boot 3 with Java 22 and Jakarta EE
ivargrimstad
0
1.1k
Git Lint
bkuhlmann
4
750
Netty Chicago Java User Group 2024-04-17
sullis
0
170
CQRS/ES avec Symfony, c’est (trop) bien !
jeremyfreeagent
1
640
Folding Cheat Sheet #3
philipschwarz
PRO
0
120
Java 22 Overview
kishida
1
180
Code Reviews
bkuhlmann
4
890
Rubyでたのしむクリエイティブコーディング/Enjoy Creative coding with Ruby
chobishiba
1
170
ゆるい個人開発のススメ
kuroppe1819
10
980
Semantic search with Django and pgvector
pauloxnet
0
240
MetricKitで予期せぬ終了を検知する話 / Detect unexpected termination with MetricKit
nekowen
0
180
Site Reliability Engineering for GMO
pyama86
7
1k
Featured
See All Featured
The Art of Programming - Codeland 2020
erikaheidi
41
12k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
115
18k
How GitHub Uses GitHub to Build GitHub
holman
468
290k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
6
990
It's Worth the Effort
3n
180
27k
jQuery: Nuts, Bolts and Bling
dougneiner
59
7.1k
Build your cross-platform service in a week with App Engine
jlugia
225
17k
Designing for Performance
lara
601
67k
10 Git Anti Patterns You Should be Aware of
lemiorhan
647
58k
Git: the NoSQL Database
bkeepers
PRO
422
63k
Designing the Hi-DPI Web
ddemaree
276
33k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
13
1.5k
Transcript
gophers := [4]string{"blue", "pink", "purple", "blue"}
Arrays are fixed-length planets := [8]string{ "Mercury", "Venus", "Earth", "Mars",
"Jupiter", "Saturn", "Uranus", "Neptune", } 0 1 2 3 4 5 6 7
Arrays are values planetBackup := planets // make way
for an interstellar bypass planets[2] = “" fmt.Println(planets) // [Mercury Venus Mars Jupiter Saturn Uranus Neptune] fmt.Println(planetBackup) // [Mercury Venus Earth Mars Jupiter Saturn Uranus Neptune]
Slicing makes a window terrestrial := planets[0:4] gasGiants := planets[4:6]
iceGiants := planets[6:8] 0 1 2 3 4 5 6 7 terrestrial gasGiants iceGiants
Half-open range planets[2:3] // [Earth] < 3 • starts at
index 2 • excludes index 3
Defaults terrestrial := planets[:4] gasGiants := planets[4:6] iceGiants := planets[6:]
allPlanets := planets[:]
Making a slice // allPlanets := planets[:] planets := []string{
"Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune", }
Append to slices planets = append(planets, "Pluto") 0 1 2
3 4 5 6 7 8 9 10 11 12 13 14 15 fmt.Println(len(planets), cap(planets)) // 9 16
Append again planets = append(planets, "Eris", "Sedna") fmt.Println(len(planets), cap(planets))
// 11 16
http://blog.golang.org/go-slices-usage-and-internals https://github.com/golang/go/wiki/SliceTricks More reading