Download - Go Tutorial
-
8/8/2019 Go Tutorial
1/16
Introduction----
This document is a tutorial introduction to the basics of the Go programminglanguage, intended for programmers familiar with C or C++. It is not a comprehensive
guide to the language; at the moment the document closest to that is thelanguage specification.After you've read this tutorial, you might want to look atEffective Go,which digs deeper into how the language is used.Also, slides from a 3-day course about Go are available:Day 1,Day 2,Day 3.
The presentation here proceeds through a series of modest programs to illustratekey features of the language. All the programs work (at time of writing) and ar
echecked into the repository in the directory "/doc/progs/".
Program snippets are annotated with the line number in the original file; forcleanliness, blank lines remain blank.
Hello, World----
Let's start in the usual way:
--PROG progs/helloworld.go /package/ END
Every Go source file declares, using a "package" statement, which package it's part of.It may also import other packages to use their facilities.This program imports the package "fmt" to gain access toour old, now capitalized and package-qualified, friend, "fmt.Printf".
Functions are introduced with the "func" keyword.The "main" package's "main" function is where the program starts running (afterany initialization).
String constants can contain Unicode characters, encoded in UTF-8.
(In fact, Go source files are defined to be encoded in UTF-8.)
The comment convention is the same as in C++:
/* ... */// ...
Later we'll have much more to say about printing.
Semicolons----
You might have noticed that our program has no semicolons. In Go
code, the only place you typically see semicolons is separating theclauses of "for" loops and the like; they are not necessary afterevery statement.
-
8/8/2019 Go Tutorial
2/16
In fact, what happens is that the formal language uses semicolons,much as in C or Java, but they are inserted automaticallyat the end of every line that looks like the end of a statement. Youdon't need to type them yourself.
For details about how this is done you can see the language
specification, but in practice all you need to know is that younever need to put a semicolon at the end of a line. (You can putthem in if you want to write multiple statements per line.) As anextra help, you can also leave out a semicolon immediately beforea closing brace.
This approach makes for clean-looking, semicolon-free code. Theone surprise is that it's important to put the openingbrace of a construct such as an "if" statement on the same line asthe "if"; if you don't, there are situations that may not compileor may give the wrong result. The language forces the brace styleto some extent.
Compiling----
Go is a compiled language. At the moment there are two compilers."Gccgo" is a Go compiler that uses the GCC back end. There is also asuite of compilers with different (and odd) names for each architecture:"6g" for the 64-bit x86, "8g" for the 32-bit x86, and more. Thesecompilers run significantly faster but generate less efficient codethan "gccgo". At the time of writing (late 2009), they also havea more robust run-time system although "gccgo" is catching up.
Here's how to compile and run our program. With "6g", say,
$ 6g helloworld.go # compile; object goes into helloworld.6$ 6l helloworld.6 # link; output goes into 6.out$ 6.outHello, world; or ; o
$
Wit
"gccgo" it oo
itt
mo
t
dition .
$ gccgo
owo
d.go$ .outH
o, wo
d; o
; o
$
Ec
o----
N
xt up,
' v
ion of t
Unix uti ity "
c
o(1)":
--PROG p
og /
c
o.go /p c
g
/ END
T
i p
og
m i m but it' doing numb
of n
w t
ing . In t
t
x mp
,w
w "func" int
oduc
function. T
ywo
d "v
", "con t", nd "typ
"(not u
d y
t) o int
oduc
d
c
tion , do
"impo
t".
Notic
t
t w
c n g
oup d
c
tion of t
m
o
t intop
nt
iz
d i t , on
it
m p
in
, on in
7-10 nd 14-17.But it' not n
c
y to do o; w
cou d
v
id
-
8/8/2019 Go Tutorial
3/16
con t Sp c
= " "con t N
w in
= "\n"
T
i p
og
m impo
t t
""o "" p c
g
to cc
it "Stdout" v
i b
, of typ
"*o .Fi
". T
"impo
t" t t
m
nt i ctu y d
c
tion: in it g
n
fo
m, u
d in ou
``
o wo
d'' p
og
m,it n m
t
id
ntifi ("fmt")t
t wi b
u
d to cc
m
mb
of t
p c
g
impo
t
d f
om t
fi
(""fmt""),found in t
cu nt di cto
y o
in t nd
d oc tion.In t
i p
og
m, t
oug
, w
'v
d
opp
d t
xp icit n m
f
om t
impo
t ; by d
f u t,p c
g
impo
t
d u ing t
n m
d
fin
d by t
impo
t
d p c
g
,w
ic
by conv
ntion i of cou
t
fi
n m
it
f. Ou
``
o wo
d'' p
og
mcou d
v
id ju t "impo
t "fmt"".
You c n p
cify you
own impo
t n m
if you w nt but it' on y n
c
y if you n d to o v
n ming conf ict.
Giv
n "o .Stdout" w
c n u
it "W
it
St
ing" m
t
od to p
int t
t
ing.
H ving impo
t
d t
"f g" p c
g
, in
12 c t
g ob v
i b
to
o dt
v u
of
c
o' "-n" f g. T
v
i b
"omitN
w in
"
typ
"*boo ", point
to "boo ".
In "m in.m in", w
p
t
gum
nt ( in
20) nd t
n c t
oc t
ing v
i b
w
wi u
to bui d t
output.
T
d
c
tion t t
m
nt
t
fo
m
v
t
ing = ""
T
i i t
"v
"
ywo
d, fo ow
d by t
n m
of t
v
i b
, fo ow
d byit typ
, fo ow
d by n
qu ign nd n initi v u
fo
t
v
i b
.
Go t
i
to b
t
, nd t
i d
c
tion cou d b
o
t
n
d. Sinc
t
t
ing con t nt i of typ
t
ing, w
don't
v
to t
t
compi
t
t.W
cou d w
it
v
= ""
o
w
cou d go
v
n
o
t
nd w
it
t
idiom
:= ""
T
":=" op
to
i u
d ot in Go to
p
nt n initi izing d
c
tion.T
' on
in t
"fo
" c u
on t
n
xt in
:
--PROG p
og /
c
o.go /fo
/
T
"f g" p c
g
p
d t
gum
nt nd
ft t
non-f g
gum
nt
in i t t
t c n b
it
t
d ov
in t
obviou w y.
T
Go "fo
" t t
m
nt diff
f
om t
t of C in numb
of w y . Fi
t,
-
8/8/2019 Go Tutorial
4/16
it' t
on y ooping con t
uct; t
i no "w
i
" o
"do". S
cond,t
no p nt
on t
c u
, but t
b
c
on t
body
m nd to
y. T
m
pp i
to t
"if" nd " witc
" t t
m
nt .L t
x mp
wi
ow om
ot
w y "fo
" c n b
w
itt
n.
T
body of t
oop bui d up t
t
ing " " by pp
nding (u ing "+=")t
gum
nt nd
p
ting p c
. Aft
t
oop, if t
"-n" f g i not
t, t
p
og
m pp
nd n
w in
. Fin y, it w
it
t
u t.
Notic
t
t "m in.m in" i ni dic function wit
no tu
n typ
.It' d
fin
d t
t w y. F ing off t
nd of "m in.m in" m
n '' ucc
''; if you w nt to ign n on
ou tu
n, c
o .Exit(1)
T
"o " p c
g
cont in ot
nti fo
g
tting t
t
d; fo
in t nc
, "o .A
g " i ic
u
d by t
"f g" p c
g
to cc
t
comm nd- in
gum
nt .
An Int
ud
bout Typ
----
Go
om
f mi i
typ
uc
"int" nd "f o t", w
ic
p
ntv u
of t
'' pp
op
i t
'' iz
fo
t
m c
in
. It o d
fin
xp icit y- iz
d typ
uc
"int8", "f o t64", nd o on, p u un ign
d int
g typ
uc
"uint", "uint32",
tc. T
di tinct typ
;
v
n if "int" nd "int32" bot
32 bit in iz
,t
y not t
m
typ
. T
i o "byt
" ynonym fo
"uint8", w
ic
i t
m
nt typ
fo
t
ing .
Sp
ing of " t
ing", t
t' bui t-in typ
w
. St
ing
immut b
v u
&md
;t
y not ju t y of "byt
" v u
.
Onc
you'v
bui t t
ing v u
, you c n't c
ng
it, t
oug
of cou
you c n c
ng
t
ing v
i b
imp y by
igning it. T
i nipp
t f
om " t
ing .go" i
g cod
:
--PROG p
og / t
ing .go /
o/ /ci o/
How
v
t
fo owing t t
m
nt
i
g b
c u
t
y wou d modify " t
ing" v u
:
[0] = 'x'(*p)[1] = 'y'
In C++ t
m
, Go
t
ing
bit
i
"con
t
t
ing
", w
i
point
to t
ing
n ogou to "con t t
ing"
f
nc
.
Y
, t
point
. How
v
, Go imp ifi
t
i
u
itt
;
d on.
A
y
d
c
d i
t
i :
v
yOfInt [10]int
A
y , i
t
ing ,
v u
, but t
y
mut b
. T
i diff
f
om C, in w
ic
"
yOfInt" wou d b
u b
point
to "int".In Go, inc
y
v u
, it' m
ningfu ( nd u
fu ) to t
bout point
to
y .
T
iz
of t
y i p
t of it typ
;
ow
v
, on
c n d
c
-
8/8/2019 Go Tutorial
5/16
ic
v
i b
to
o d f nc
to ny y, of ny iz
,wit
t
m
m
nt typ
.A ic
xp
ion
t
fo
m " [ ow :
ig
]",
p
ntingt
int n y ind
x
d f
om " ow" t
oug
"
ig
-1"; t
u ting ic
i ind
x
d f
om "0" t
oug
"
ig
- ow-1".In
o
t, ic
oo
ot i
y but wit
no
xp icit iz
("[]" v . "[10]") nd t
y
f
nc
gm
nt of n und ying, u u y nonymou , gu
y. Mu tip
ic
c n
d t if t
y p
nt pi
c
of t
m
y;mu tip
y c n n
v
d t .
S ic
muc
mo common in Go p
og
m t
n gu
y ; t
y' mo f
xib
,
v
f nc
m ntic , nd
ffici
nt. W
t t
y c
i t
p
ci
cont
o of to
g
yout of gu
y; if you w nt to
v
und d
m
nt of n y to d wit
in you
t
uctu , you
ou d u
gu
y. To c t
on
, u
compound v u
con t
ucto
&md
; n
xp
ion fo
m
d
f
om
typ
fo
ow
d by
b
c
-bound
d
xp
ion
i
t
i
:
[3]int{1,2,3}
In t
i c
t
con t
ucto
bui d n y of 3 "int ".
W
n p
ing n y to function, you mo t
w y w nt
to d
c t
fo
m p
m
t to b
ic
. W
n you c t
function, ic
t
y to c t
(
ffici
nt y) ic
f nc
nd p
t
t.By d
f u t, t
ow nd upp bound of ic
m tc
t
nd of t
xi ting obj
ct, o t
conci
not tion "[:]"wi ic
t
w
o
y.
U ing ic
on
c n w
it
t
i function (f
om " um.go"):
--PROG p
og / um.go / um/ /^}/
Not
ow t
tu
n typ
("int") i d
fin
d fo
" um()" by t ting it ft
t
p
m
t
i t.
To c t
function, w
ic
t
y. T
i int
ic t
c (w
'
ow imp
w y in mom
nt) con t
uct n
y nd ic
it:
:=
um([3]int{1,2,3}[:])
If you
c
ting
gu
y but w nt t
compi
to count t
m
nt fo
you, u
"..." t
y iz
:
:= um([...]int{1,2,3}[:])
T
t' fu i
t
n n
c
y, t
oug
.In p
ctic
, un
you'
m
ticu ou bout to
g
yout wit
in d t t
uctu
, ic
it
f&md
;u ing
mpty b
c
t wit
no iz
&md
;i you n
d:
:= um([]int{1,2,3})
T
o m p , w
ic
you c n initi iz
i
t
i :
-
8/8/2019 Go Tutorial
6/16
m := m p[ t
ing]int{"on
":1 , "two":2}
T
bui t-in function "
n()", w
ic
tu
n numb
of
m
nt ,m
it fi
t pp
nc
in " um". It wo
on t
ing ,
y , ic
, m p , nd c
nn
.
By t
w y, not
t
ing t
t wo
on t
ing ,
y , ic
, m p nd c
nn
i t
"
ng
" c u
on "fo
" oop . In t
d of w
iting
fo
i := 0; i & t;
n( ); i++ { ... }
to oop ov t
m
nt of ic
(o
m p o
...) , w
cou d w
it
fo
i, v :=
ng
{ ... }
T
i ign "i" to t
ind
x nd "v" to t
v u
of t
ucc
iv
m
nt of t
t
g
t of t
ng
. S
<
f='/doc/
ff
ctiv
_go.
tm '>Eff
ctiv
Gofo
mo
x mp
of it u
.
An Int ud
bout A oc tion
----
Mo t typ
in Go v u
. If you
v
n "int" o
" t
uct"o
n y,
ignm
ntcopi
t
cont
nt of t
obj
ct.To oc t
n
w v
i b
, u
"n
w()", w
ic
tu
n point to t
oc t
d to
g
.
typ
T t
uct { , b int }v
t *T = n
w(T)
o
t
mo
idiom tic
t := n
w(T)
Som
typ
&md
;m p , ic
, nd c
nn
(
b
ow)&md
;
v
f
nc
m ntic .If you'
o ding ic
o
m p nd you modify it cont
nt , ot
v
i b
f
ncing t
m
und
ying d t wi
t
modific tion. Fo
t
t
typ
you w nt to u
t
bui t-in function "m
()":
m := m
(m p[ t
ing]int)
T
i t t
m
nt initi iz
n
w m p
dy to to
nt
i
.If you ju t d
c
t
m p, in
v
m m p[ t
ing]int
it c
t
"ni "
f
nc
t
t c nnot
o d nyt
ing. To u
t
m p,you mu t fi
t initi iz
t
f
nc
u ing "m
()" o
by ignm
nt f
om n
xi ting m p.
Not
t
t "n
w(T)"
tu
n typ
"*T" w
i
"m
(T)"
tu
n typ
"T". If you (mi t
n y) oc t
f
nc
obj
ct wit
"n
w()",you
c
iv
point
to ni
f
nc
,
quiv
nt to
d
c
ing n uniniti iz
d v
i b
nd t
ing it dd
.
An Int
ud
bout Con t nt
-
8/8/2019 Go Tutorial
7/16
----
A t
oug
int
g
com
in ot of iz
in Go, int
g
con t nt do not.T
no con t nt i
"0LL" o
"0x0UL". In t
d, int
g
con t nt
v u t
d
g
-p ci ion v u
t
tc n ov f ow on y w
n t
y
ign
d to n int
g v
i b
wit
too itt
p
ci ion to
p
nt t
v u
.
con t
dEig
t = (1 & t;& t; 100) >> 97 //
g
T
nu nc
t
t d
v
di
ction to t
g
of t
ngu g
p
cific tion but
om
i u t
tiv
x mp
:
v
uint64 = 0 //
typ
uint64, v u
0 := uint64(0) //
quiv
nt; u
"conv
ion"i := 0x1234 // i g
t d
f u t typ
: intv
j int = 1
6 //
g - 1000000 i p
nt b
in n intx := 1.5 // f o ti3div2 := 3/2 // int
g
divi ion -
u t i 1
f3div2 := 3./2. // f
o
ting point divi
ion -
u
t i
1.5
Conv ion on y wo
fo
imp
c
uc
conv ting "int " of on
ign o
iz
to not
, nd b
tw
n "int " nd "f o t ", p u f
w ot
imp
c
. T
no utom tic num ic conv ion of ny
ind in Go,ot
t
n t
t of m
ing con t nt
v
conc t
iz
nd typ
w
n
ign
d to v
i b
.
An I/O P c
g
----
N
xt w
' oo
t imp
p c
g
fo
doing fi
I/O wit
t
u u o
t of op
n/c o
/ d/w
it
int f c
. H ' t
t
t of "fi
.go":
--PROG p
og /fi
.go /p c
g
/ /^}/
T
fi
t f
w in
d
c
t
n m
of t
p c
g
&md
;"fi
"&md
; nd t
n impo
t two p c
g
. T
"o "p c
g
id
t
diff
nc
b
tw
n v
iou op
ting y t
m to giv
con i t
nt vi
w of fi
nd o on;
w
'
going to u
it
o
nd ing uti iti
nd
p
oduc
t
udim
nt of it fi
I/O.
T
ot
it
m i t
ow-
v
,
xt
n " y c " p c
g
, w
ic
p
ovid
p
imitiv
int
f c
to t
und
ying op
ting y t
m' c .
N
xt i typ
d
finition: t
"typ
"
ywo
d int
oduc
typ
d
c
tion,in t
i c
d t t
uctu
c
d "Fi
".To m
t
ing itt
mo
int
ting, ou
"Fi
" inc ud
t
n m
of t
fi
t
t t
fi
d
c
ipto
f
to.
B
c u
"Fi
" t
t wit
c pit
tt
, t
typ
i v i b
out id
t
p c
g
,t
t i , by u
of t
p c
g
. In Go t
u
bout vi ibi ity of info
m tion i
imp
: if n m
(of top-
v
typ
, function, m
t
od, con t nt o
v
i b
,o
of t
uctu
fi
d o
m
t
od) i c pit iz
d, u
of t
p c
g
m y
it. Ot
wi
, t
n m
nd
nc
t
t
ing b
ing n m
d i vi ib
on y in id
t
p c
g
in w
ic
-
8/8/2019 Go Tutorial
8/16
it i d
c d. T
i i mo t
n conv
ntion; t
u
i
nfo
c
d by t
compi .In Go, t
t
m fo
pub ic y vi ib
n m
i ''
xpo
t
d''.
In t
c
of "Fi
", it fi
d ow c
nd o invi ib
to u ,but w
wi oon giv
it om
xpo
t
d, upp
-c
m
t
od .
Fi
t, t
oug
,
i f cto
y to c t
"Fi
":
--PROG p
og /fi
.go /n
wFi
/ /^}/
T
i tu
n point to n
w "Fi
" t
uctu wit
t
fi
d
c
ipto
nd n m
fi
d in. T
i cod
u
Go' notion of ''compo it
it
'', n ogou tot
on
u
d to bui d m p nd y , to con t
uct n
w
p- oc t
dobj
ct. W
cou d w
it
n := n
w(Fi
)
n.fd = fdn.n m
= n m
tu
n n
but fo
imp
t
uctu i
"Fi
" it'
i to tu
n t
dd of compo it
it ,
i don
on in
21.
W
c n u
t
f cto
y to con t
uct om
f mi i
,
xpo
t
d v
i b
of typ
"*Fi
":
--PROG p
og /fi
.go /v
/ /^.$/
T
"n
wFi
" function w not
xpo
t
d b
c u
it' int n . T
p
op ,
xpo
t
d f cto
y to u
i "Op
n":
--PROG p
og /fi
.go /func.Op
n/ /^}/
T
numb
of n
w t
ing in t
f
w in
. Fi
t, "Op
n"
tu
n mu tip
v u
, "Fi
" nd n
o
(mo
bout
o
in mom
nt).W
d
c
t
mu ti-v u
tu
n p
nt
iz
d i t of d
c
tion ; ynt ctic yt
y oo
ju t i
cond p
m
t
i t. T
function" y c .Op
n" o
mu ti-v u
tu
n, w
ic
w
c n g
b wit
t
mu ti-v
i b
d
c
tion on in
31; it d
c
"
" nd "
" to
o d t
two v u
,
bot
of typ
"int" (
t
oug
you'd
v
to
oo
t t
"
y
c
" p
c
g
to
t
t). Fin y, in
35
tu
n two v u
: point
to t
n
w "Fi
" nd t
o
. If " y c .Op
n" f i , t
fi
d
c
ipto
"
" wi b
n
g tiv
nd "n
wFi
" wi
tu
n "ni ".
About t
o
o
: T
"o " ib
y inc ud
g
n
notion of n
o
.It' good id
to u
it f ci ity in you
own int
f c
, w
do
, fo
con i t
nt
o
nd ing t
oug
out Go cod
. In "Op
n" w
u
conv
ion to t
n t
Unix' int
g
"
no" v u
into t
int
g
typ
"o .E
no", w
ic
imp
m
nt "o .E
o
".
Now t
t w
c n bui d "Fi
", w
c n w
it
m
t
od fo
t
m. To d
c
m
t
od of typ
, w
d
fin
function to
v
n
xp icit
c
iv
of t
t typ
, p c
din p
nt
b
fo
t
function n m
. H
om
m
t
od fo
"*Fi
",
c
of w
ic
d
c
c
iv
v
i b
"fi
".
-
8/8/2019 Go Tutorial
9/16
--PROG p
og /fi
.go /C o
/ END
T
i no imp icit "t
i " nd t
c
iv
v
i b
mu t b
u
d to cc
m
mb of t
t
uctu . M
t
od not d
c d wit
int
" t
uct" d
c
tion it
f. T
" t
uct" d
c
tion d
fin
on y d t m
mb
.
In f ct, m
t
od c n b
c
t
d fo
mo t ny typ
you n m
, uc
n int
g
o
y, not ju t fo
" t
uct ". W
' n
x mp
wit
y t .
T
"St
ing" m
t
od i o c
d b
c u
of p
inting conv
ntion w
' d
c
ib
t .
T
m
t
od u
t
pub ic v
i b
"o .EINVAL" to
tu
n t
("o .E
o
"v ion of t
) Unix o
cod
"EINVAL". T
"o " ib
y d
fin
t nd
d
t of uc
o
v u
.
W
c n now u
ou
n
w p c
g
:
--PROG p
og /
owo
d3.go /p c
g
/ END
T
''"./"'' in t
impo
t of ''"./fi
"'' t
t
compi
to u
ou
own p c
g
t
t
n om
t
ing f
om t
di cto
y of in t
d p c
g
.(A
o, ''"fi
.go"'' mu t b
compi
d b
fo w
c n impo
t t
p c
g
.)
Now w
c n compi
nd
un t
p
og
m:
$ 6g fi
.go # compi
fi
p c
g
$ 6g
owo
d3.go # compi
m in p c
g
$ 6 -o
owo
d3
owo
d3.6 # in
- no n
d to m
ntion "fi
"$
owo
d3
o, wo
dc n't op
n fi
;
=No uc
fi
o
di
cto
y$
Rotting c t ----
Bui ding on t
"fi
" p c
g
,
' imp
v
ion of t
Unix uti ity "c t(1)","p
og /c t.go":
--PROG p
og /c t.go /p c
g
/ END
By now t
i
ou d b
y to fo ow, but t
" witc
" t t
m
nt int
oduc
om
n
w f
tu
. Li
"fo
" oop, n "if" o
" witc
" c n inc ud
niniti iz tion t t
m
nt. T
" witc
" on in
18 u
on
to c
t
v
i b
"n
" nd "
" to
o d t
tu
n v u
f
om "f.R
d()". (T
"if" on in
25
t
m
id
.) T
" witc
" t t
m
nt i g
n
: it
v u t
t
c
f
om top to bottom oo
ing fo
t
fi
t c
t
t m tc
t
v u
; t
c
xp
ion don't n
d to b
con t nt o
v
n int
g
, ong t
y
v
t
m
typ
.
Sinc
t
" witc
" v u
i ju t "t
u
", w
cou d
v
it off&md
; i o
t
itu tionin "fo
" t t
m
nt, mi ing v u
m
n "t
u
". In f ct, uc
" witc
"i fo
m of "if-
" c
in. W
i
w
'
, it
ou d b
m
ntion
d t
t in
-
8/8/2019 Go Tutorial
10/16
" witc
" t t
m
nt
c
"c
"
n imp icit "b
".
Lin
25 c "W
it
()" by icing t
incoming buff
, w
ic
i it
f ic
.S ic
p
ovid
t
t nd
d Go w y to
nd
I/O buff
.
Now
t' m
v
i nt of "c t" t
t option
y do
"
ot13" on it input.It'
y to do by ju t p
oc
ing t
byt
, but in t
d w
wi
xp oit
Go' notion of n int
f c
.
T
"c t()" ub
outin
u
on y two m
t
od of "f": "R
d()" nd "St
ing()", o
t' t
t by d
fining n int
f c
t
t
x ct y t
o
two m
t
od .H i cod
f
om "p
og /c t_
ot13.go":
--PROG p
og /c t_
ot13.go /typ
. d / /^}/
Any typ
t
t
t
two m
t
od of " d "&md
; g
d
of w
t
v
ot
m
t
od t
typ
m y o
v
&md
;i id to imp
m
nt t
int f c
. Sinc
"fi
.Fi
" imp
m
nt t
m
t
od , it imp
m
nt t
"
d
" int
f c
. W
cou d tw
t
"c t" ub
outin
to cc
pt "
d
"
in
t
d of
"*fi
.Fi
"
nd it wou
d wo
ju
t fin
, but
t'
mb
i
itt
fi
t by w
iting
cond typ
t
t imp
m
nt " d ", on
t
t w
p n
xi ting "
d
" nd do
"
ot13" on t
d t . To do t
i , w
ju t d
fin
t
typ
nd imp
m
nt t
m
t
od nd wit
no ot
boo
ping,w
v
cond imp
m
nt tion of t
" d " int f c
.
--PROG p
og /c t_
ot13.go /typ
.
ot t
13/ /
nd.of.
ot t
13/
(T
"
ot13" function c
d on in
42 i t
ivi nd not wo
t
p
oducing
.)
To u
t
n
w f
tu , w
d
fin
f g:
--PROG p
og /c t_
ot13.go /
ot13F g/
nd u
it f
om wit
in mo t y unc
ng
d "c t()" function:
--PROG p
og /c t_
ot13.go /func.c t/ /^}/
(W
cou d o do t
w
pping in "m in" nd
v
"c t()" mo t y on
,
xc
ptfo
c
nging t
typ
of t
gum
nt; con id
t
t n
x
ci
.)Lin
56 t
oug
58
t it up: If t
"
ot13" f g i t
u
, w
p t
"
d
"w
c
iv
d into "
ot t
13" nd p
oc
d. Not
t
t t
int
f c
v
i b
v
u
, not point
: t
gum
nt i
of typ
"
d
", not "*
d
", v
n t
oug
und
t
cov
it
o d point
to " t
uct".
H
it i in ction:
$
c
o bcd
fg
ij
mnopq
tuvwxyz ./c t bcd
fg
ij
mnopq
tuvwxyz$
c
o bcd
fg
ij
mnopq
tuvwxyz ./c t --
ot13nopq
tuvwxyz bcd
fg
ij
m$
/pp>F n of d
p
nd
ncy inj
ction m y t
c
f
om
ow
i y int
f c
ow u to ub titut
t
imp
m
nt tion of fi
d
c
ipto
.
-
8/8/2019 Go Tutorial
11/16
Int f c
di tinctiv
f
tu of Go. An int f c
i imp
m
nt
d by typ
if t
typ
imp
m
nt t
m
t
od d
c d in t
int f c
.T
i m
n t
t typ
m y imp
m
nt n
bit
y numb
of diff
nt int
f c
.T
i no typ
i
c
y; t
ing c n b
muc
mo d
oc,
w
w wit
"
ot13". T
typ
"fi
.Fi
" imp
m
nt " d "; it cou d
oimp
m
nt "w
it
", o
ny ot
int
f c
bui t f
om it m
t
od t
t
fit t
cu
nt itu tion. Con id
t
mpty int
f c
typ
Empty int
f c
{}
Ev y typ
imp
m
nt t
mpty int f c
, w
ic
m
itu
fu fo
t
ing i
cont in
.
So
ting----
Int
f
c
p
ovid
imp
fo
m of po
ymo
p
i
m. T
y comp
t
y
p
t
t
d
finition of w
t n obj
ct do
f
om
ow it do
it,
owingdi tinct imp
m
nt tion to b
p
nt
d t diff nt tim
by t
m
int
f c
v
i b
.
A n
x mp
, con id t
i imp
o
t go
it
m t
n f
om "p
og / o
t.go":
--PROG p
og / o
t.go /func.So
t/ /^}/
T
cod
n d on y t
m
t
od , w
ic
w
w
p into o
t' "Int f c
":
--PROG p
og / o
t.go /int f c
/ /^}/
W
c n pp y "So
t" to ny typ
t
t imp
m
nt "L
n", "L
", nd "Sw p".T
" o
t" p c
g
inc ud
t
n
c
y m
t
od to ow o
ting of
y of int
g
, t
ing ,
tc.;
' t
cod
fo
y of "int"
--PROG p
og / o
t.go /typ
.*IntA
y/ /Sw p/
H
w
m
t
od d
fin
d fo
non-" t
uct" typ
. You c n d
fin
m
t
od fo
ny typ
you d
fin
nd n m
in you
p c
g
.
And now
outin
to t
t it out, f
om "p
og / o
tm in.go". T
i u
function in t
" o
t" p c
g
, omitt
d
fo
b
vity,to t
t t
t t
u t i o
t
d.
--PROG p
og / o
tm in.go /func.int / /^}/
If w
v
n
w typ
w
w nt to b
b
to o
t, w
n
d to do i to imp
m
nt t
t
m
t
od fo
t
t typ
, i
t
i :
--PROG p
og / o
tm in.go /typ
.d y/ /Sw p/
P
inting----
T
x mp
of fo
m tt
d p
inting o f
v
b
n mod
t. In t
i
ction
w
' t
bout
ow fo
m tt
d I/O c n b
don
w
in Go.
W
'v
n imp
u
of t
p c
g
"fmt", w
ic
-
8/8/2019 Go Tutorial
12/16
imp
m
nt "P
intf", "Fp
intf", nd o on.Wit
in t
"fmt" p c
g
, "P
intf" i d
c d wit
t
i ign tu :
P
intf(fo
m t t
ing, v ...int
f c
{}) (n int,
no o .E
o
)
T
to
n "..." int
oduc
v
i b
-
ngt
gum
nt i t t
t in C wou db
nd
d u ing t
" td
g.
" m c
o .
In Go, v
i dic function
p
d ic
of t
gum
nt of t
p
cifi
d typ
. In "P
intf"' c
, t
d
c
tion y "...int f c
{}" o t
ctu
typ
i ic
of
mpty int f c
v u
, "[]int f c
{}"."P
intf" c n
x min
t
gum
nt by it
ting ov
t
ic
nd, fo
c
m
nt, u ing typ
witc
o
t
f
ction ib
yto int p t t
v u
.It' off topic
but uc
un-tim
typ
n y i
p
xp in om
of t
nic
p
op
ti
of Go' "P
intf",du
to t
bi ity of "P
intf" to di cov t
typ
of it
gum
nt dyn mic y.
Fo
x mp
, in C
c
fo
m t mu t co
pond to t
typ
of it
gum
nt. It'
i
in m
ny c
in Go. In
t
d of "%
ud" youc n ju t y "%d"; "P
intf"
now t
iz
nd ign
dn
of t
int
g nd c n do t
ig
t t
ing fo
you. T
nipp
t
--PROG p
og /p
int.go 'NR==10' 'NR==11'
p
int
18446744073709551615 -1
In f ct, if you' zy t
fo
m t "%v" wi
p
int, in imp
pp
op
i t
ty
, ny v u
,
v
n n y o
t
uctu . T
output of
--PROG p
og /p
int.go 'NR==14' 'NR==20'
i
18446744073709551615 {77 Sun
t St
ip} [1 2 3 4]
You c n d
op t
fo
m tting tog
t
if you u
"P
int" o
"P
int n"in t
d of "P
intf". T
o
outin
do fu y utom tic fo
m tting.T
"P
int" function ju t p
int it
m
nt out u ing t
quiv
ntof "%v" w
i
"P
int n" in
t p c
b
tw
n
gum
nt nd dd n
w in
. T
output of
c
of t
two in
i id
ntic to t
t of t
"P
intf" c bov
.
--PROG p
og /p
int.go 'NR==21' 'NR==22'
If you
v
you
own typ
you'd i
"P
intf" o
"P
int" to fo
m t,ju t giv
it "St
ing()" m
t
od t
t
tu
n t
ing. T
p
int
outin
wi
x min
t
v u
to inqui
w
t
it imp
m
nt t
m
t
od nd if o, u
it
t
t
n om
ot
fo
m tting.H
' imp
x mp
.
--PROG p
og /p
int_ t
ing.go 'NR==9' END
Sinc
"*t
tTyp
"
"St
ing()" m
t
od, t
d
f u t fo
m tt
fo
t
t typ
wi u
it nd p
oduc
t
output
77 Sun
t St
ip
-
8/8/2019 Go Tutorial
13/16
Ob v
t
t t
"St
ing()" m
t
od c "Sp
int" (t
obviou Gov
i nt t
t tu
n t
ing) to do it fo
m tting; p
ci fo
m tt c n u
t
"fmt" ib
y
cu
iv
y.
Anot
f
tu of "P
intf" i t
t t
fo
m t "%T" wi p
int t
ing p
nt tion of t
typ
of v u
, w
ic
c n b
ndy w
n d
buggingpo ymo
p
ic cod
.
It' po ib
to w
it
fu cu tom p
int fo
m t wit
f g nd p ci ion nd uc
, but t
t' g
tting itt
off t
m in t
d o w
'
v
it n
xp o
tion
x
ci
.
You mig
t
, t
oug
,
ow "P
intf" c n t
w
t
typ
imp
m
nt t
"St
ing()" m
t
od. Actu
y w
t it do
i
if t
v u
c nb
conv
t
d to n int
f c
v
i b
t
t imp
m
nt t
m
t
od.Sc
m tic y, giv
n v u
"v", it do
t
i :
typ
St
ing
int
f c
{
St
ing()
t
ing}
, o
:= v.(St
ing
) // T
t w
t
v imp
m
nt "St
ing()"if o
{
u t = .St
ing()}
{
u t = d
f u tOutput(v)}
T
cod
u
``typ
tion'' ("v.(St
ing )") to t
t if t
v u
to din"v" ti fi
t
"St
ing " int f c
; if it do
, " "
wi b
com
n int
f c
v
i b
imp
m
nting t
m
t
od nd "o
" wi b
"t
u
". W
t
n u
t
int
f c
v
i b
to c t
m
t
od.(T
''comm , o
'' p tt
n i Go idiom u
d to t
t t
ucc
ofop
tion uc
typ
conv
ion, m p upd t
, communic tion , nd o on, t
oug
t
i i t
on y pp
nc
in t
i tuto
i .)If t
v u
do
not ti fy t
int
f c
, "o
" wi b
f
.
In t
i nipp
t t
n m
"St
ing
" fo ow t
conv
ntion t
t w
dd ''[
]
''to int
f c
d
c
ibing imp
m
t
od
t i
t
i .
On
t w
in
. To comp
t
t
uit
, b
id
"P
intf"
tc. nd "Sp
intf"
tc., t
o "Fp
intf"
tc. Un i
in C, "Fp
intf"' fi
t
gum
nt i
not
fi
. In
t
d, it i
v
i
b
of typ
"io.W
it
", w
ic
i
nint
f c
typ
d
fin
d in t
"io" ib
y:
typ
W
it
int
f c
{W
it
(p []byt
) (n int,
o .E
o
)}
(T
i int
f c
i not
conv
ntion n m
, t
i tim
fo
"W
it
"; t
o"io.R
d
", "io.R
dW
it
", nd o on.)T
u you c n c "Fp
intf" on ny typ
t
t imp
m
nt t nd
d "W
it
()"m
t
od, not ju t fi
but o n
two
c
nn
, buff
, w
t
v
you w nt.
P
im
numb
----
-
8/8/2019 Go Tutorial
14/16
Now w
com
to p
oc
nd communic tion&md
;concu nt p
og
mming.It' big ubj
ct o to b
b
i
f w
um
om
f mi i
ity wit
t
topic.
A c ic p
og
m in t
ty
i p
im
i
v
.(T
i
v
of E
to t
n
i comput tion
y mo
ffici
nt t
nt
go
it
m p
nt
d
, but w
mo
int
t
d in concu
ncy t
n go
it
mic t t
mom
nt.)It wo
by t
ing t m of t
n tu
numb nd int
oducing
qu
nc
of fi t , on
fo
c
p
im
, to winnow t
mu tip
oft
t p
im
. At
c
t
p w
v
qu
nc
of fi t
of t
p
im
o f
, nd t
n
xt numb to pop out i t
n
xt p
im
, w
ic
t
igg t
c tion of t
n
xt fi t in t
c
in.
H
' f ow di g
m;
c
box
p
nt fi t
m
nt w
o
c tion i t
igg d by t
fi
t numb t
t f ow
d f
om t
m
nt b
fo it.
&nb p;&nb p;&nb p;&nb p;&nb p;
To c t
t m of int
g , w
u
Go c
nn
, w
ic
,bo owing f
om CSP' d
c
nd nt , p
nt communic tion c
nn
t
t c n conn
ct two concu nt comput tion .In Go, c
nn
v
i b
f nc
to
un-tim
obj
ct t
tcoo
din t
t
communic tion;
wit
m p nd ic
, u
"m
" to c t
n
w c
nn
.
H i t
fi
t function in "p
og / i
v
.go":
--PROG p
og / i
v
.go /S
nd/ /^}/
T
"g
n
t
" function
nd t
qu
nc
2, 3, 4, 5, ... to it
gum
nt c
nn
, "c
", u ing t
bin
y communic tion op
to
"& t;-".C
nn
op
tion b oc
, o if t
' no
cipi
nt fo
t
v u
on "c
",t
nd op
tion wi w it unti on
b
com
v i b
.
T
"fi t
" function
t
gum
nt : n input c
nn
, n outputc
nn
, nd p
im
numb
. It copi
v u
f
om t
input to t
output, di c
ding nyt
ing divi ib
by t
p
im
. T
un
y communic tion op
to
"& t;-" (
c
iv
)
t
i
v
t
n
xt v u
on t
c
nn
.
--PROG p
og / i
v
.go /Copy.t
/ /^}/
T
g
n
to
nd fi t
x
cut
concu
nt y. Go
it own mod
of p
oc
/t
d / ig
t-w
ig
t p
oc
/co
outin
, o to void not tion confu ion w
c concu
nt y
x
cutingcomput tion in Go go
outin
. To t
t go
outin
,invo
t
function, p
fixing t
c wit
t
ywo
d "go";t
i t
t t
function
unning in p
wit
t
cu
ntcomput tion but in t
m
dd
p c
:
go um(
ug
A
y) // c cu t
um in t
b c
g
ound
If you w nt to
now w
n t
c cu tion i don
, p c
nn
on w
ic
it c n
po
t b c
:
-
8/8/2019 Go Tutorial
15/16
c
:= m
(c
n int)go um(
ug
A y, c
)// ... do om
t
ing
fo
w
i
u t := & t;-c
// w it fo
, nd
t
i
v
,
u t
B c
to ou
p
im
i
v
. H '
ow t
i
v
pip
in
i titc
dtog
t
:
--PROG p
og / i
v
.go /func.m in/ /^}/
Lin
29 c
t
t
initi c
nn
to p to "g
n
t
", w
ic
itt
n t
t up. A
c
p
im
pop out of t
c
nn
, n
w "fi t "i dd
d to t
pip
in
nd it output b
com
t
n
w v u
of "c
".
T
i
v
p
og
m c n b
tw
d to u
p tt n commonin t
i ty
of p
og
mming. H i v
i nt v ionof "g
n t
", f
om "p
og / i
v
1.go":
--PROG p
og
/
i
v
1.go /func.g
n
t
/ /^}/
T
i v ion do
t
tup int n
y. It c t
t
outputc
nn
, unc
go
outin
unning function it
, nd tu
n t
c
nn
to t
c . It i f cto
y fo
concu nt
x
cution, t
ting t
go
outin
nd tu
ning it conn
ction.
T
function it not tion ( in
12-16) ow u to con t
uct n nonymou function nd invo
it on t
pot. Notic
t
t t
oc v
i b
"c
" i v i b
to t
function it nd iv
on
v
n ft "g
n t
" tu
n .
T
m
c
ng
c n b
m d
to "fi t ":
--PROG p
og / i
v
1.go /func.fi t
/ /^}/
T
" i
v
" function' m in oop b
com
imp
nd c
u t, nd w
i
w
'
t it
t' tu
n it into f cto
y too:
--PROG p
og / i
v
1.go /func. i
v
/ /^}/
Now "m in"' int
f c
to t
p
im
i
v
i c
nn
of p
im
:
--PROG p
og / i
v
1.go /func.m in/ /^}/
Mu
tip
xing----
Wit
c
nn
, it' po ib
to
v
mu tip
ind
p
nd
nt c i
nt go
outin
wit
outw
iting n
xp icit mu tip
x
. T
t
ic
i to
nd t
v
c
nn
in t
m
g
,w
ic
it wi t
n u
to
p y to t
o
igin
nd
.A
i tic c i
nt-
v
p
og
m i ot of cod
, o
i v
y imp
ub titut
to i u t
t
t
id
. It t
t by d
fining "
qu
t" typ
, w
ic
mb
d c
nn
t
t wi b
u
d fo
t
p y.
--PROG p
og /
v
.go /typ
.
qu
t/ /^}/
-
8/8/2019 Go Tutorial
16/16
T
v wi b
t
ivi : it wi do imp
bin
y op tion on int
g . H
' t
cod
t
t invo
t
op
tion nd
pond to t
qu
t:
--PROG p
og / v .go /typ
.binOp/ /^}/
Lin
14 d
fin
t
n m
"binOp" to b
function t
ing two int
g
nd
tu
ning t
i
d.
T
" v "
outin
oop fo v , c
iving qu
t nd, to void b oc
ing du
to ong-
unning op tion, t
ting go
outin
to do t
ctu wo
.
--PROG p
og / v .go /func. v / /^}/
W
con t
uct v in f mi i
w y, t
ting it nd tu
ning c
nn
conn
ct
d to it:
--PROG p
og /
v
.go /func. t
tS
v
/ /^}/
H ' imp
t
t. It t
t v wit
n ddition op to
nd
nd out"N"
qu
t wit
out w iting fo
t
p i
. On y ft
t
qu
t
ntdo
it c
c
t
u t .
--PROG p
og / v .go /func.m in/ /^}/
On
nnoy nc
wit
t
i p
og
m i t
t it do
n't
ut down t
v c
n y;w
n "m in" tu
n t
numb of ing ing go
outin
b oc
d on communic tion. To o v
t
i ,
w
c n p
ovid
cond, "quit" c
nn
to t
v
:
--PROG p
og /
v
1.go /func. t
tS
v
/ /^}/
It p
t
quit c
nn
to t
"
v
" function, w
ic
u
it i
t
i :
--PROG p
og /
v
1.go /func.
v
/ /^}/
In id
"
v
", t
"
ct" t t
m
nt c
oo
w
ic
of t
mu tip
communic tion
i t
d by it c
c n p
oc
d. If
b oc
d, it w it unti on
c n p
oc
d; if
mu
tip
c
n p
oc
d, it c
oo
on
t
ndom. In t
i
in
t
nc
, t
"
ct" ow t
v
to
ono
qu
t unti it
c
iv
quit m
g
, t w
ic
point it
tu
n , t
min ting it
x
cution.
A t
t'
ft i to t
ob
t
"quit" c
nn
t t
nd of m in:
--PROG p
og /
v
1.go / dd
,.quit/...--PROG p
og /
v
1.go /quit....t
u
/
T
' ot mo
to Go p
og
mming nd concu
nt p
og
mming in g
n
but t
i quic
tou
ou d giv
you om
of t
b ic .