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
120
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
94
Go and Node.js: a comparison
nathany
1
180
Diet Hacks
nathany
2
340
Go 1.6 and HTTP/2
nathany
3
120
Upgrading Rails Redux
nathany
1
84
GopherCon recap
nathany
0
150
Go Functions
nathany
0
92
Go Types
nathany
2
120
Go Packages
nathany
2
520
Other Decks in Programming
See All in Programming
苦しいTiDBへの移行を乗り越えて快適な運用を目指す
leveragestech
0
130
How mixi2 Uses TiDB for SNS Scalability and Performance
kanmo
34
13k
SpringBoot3.4の構造化ログ #kanjava
irof
2
970
『品質』という言葉が嫌いな理由
korimu
0
160
定理証明プラットフォーム lapisla.net
abap34
1
1.8k
社内フレームワークとその依存性解決 / in-house framework and its dependency management
vvakame
1
550
AWSマネコンに複数のアカウントで入れるようになりました
yuhta28
2
160
Linux && Docker 研修/Linux && Docker training
forrep
23
4.5k
Spring gRPC について / About Spring gRPC
mackey0225
0
220
2,500万ユーザーを支えるSREチームの6年間のスクラムのカイゼン
honmarkhunt
6
5.1k
個人アプリを2年ぶりにアプデしたから褒めて / I just updated my personal app, praise me!
lovee
0
340
最近のVS Codeで気になるニュース 2025/01
74th
1
250
Featured
See All Featured
Gamification - CAS2011
davidbonilla
80
5.1k
Six Lessons from altMBA
skipperchong
27
3.6k
VelocityConf: Rendering Performance Case Studies
addyosmani
328
24k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
656
59k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
6
540
BBQ
matthewcrist
86
9.5k
Music & Morning Musume
bryan
46
6.3k
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
GitHub's CSS Performance
jonrohan
1030
460k
For a Future-Friendly Web
brad_frost
176
9.5k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
49k
A Tale of Four Properties
chriscoyier
158
23k
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