6. Respected
Parents
Ken
Thompson
Unix,
B,
UTF-‐8,
Plan
9
Rob
Pike
Plan
9,
UTF-‐8,
Limbo,
Unix
team
The
Unix
Programming
Environment
The
PracKce
of
Programming
7. History
Rob
Pike
Ken
Thompson
Robert
Griesmer
Start
Late
2007
Public
2009
Go
1.0
March
2012
Go
1.1
May
2013
8.
9. Hello
package
main
import
"fmt"
func
greet()
{
fmt.Println("Hello,
I
love
you,
won’t
you
tell
me
your
name?”)
}
func
main()
{
greet()
}
10. Web
Server
package
main
import
(
"fmt"
"net/h8p"
)
func
handler(w
h8p.ResponseWriter,
r
*h8p.Request)
{
fmt.Fprine(w,
”Request
from
%s",
r.URL.Path[1:])
}
func
main()
{
h8p.HandleFunc("/",
handler)
h8p.ListenAndServe(":8080",
nil)
}
19. Custom
Types
type
Name
string
type
Person
struct
{
first
Name
last
Name
}
type
Hero
struct
{
Person
power
string
}
type
Crowd
struct
{
people
[]Person
}
28. go
rouKnes
var
a
string
func
Init()
{
a
=
"finally
started"
return
}
func
doSomethingElse()
{
//
…
}
func
Simple()
string{
go
Init()
doSomethingElse()
return
a
}
29. tradiKonal
var
(
a
string
wg
sync.WaitGroup
)
func
Init()
{
defer
wg.Done()
a
=
"finally
started"
}
func
Simple()
string{
wg.Add(1)
go
Init()
wg.Wait()
//
do
something
else
return
a
}
30. channel
package
channel
var
(
a
string
ready
chan
bool
)
func
Init()
{
a
=
"finally
started"
ready
<-‐
true
}
func
Simple()
string{
ready
=
make(chan
bool)
go
Init()
//
do
something
else
<-‐ready
return
a
}
31. Producer
/
Consumer
func
producer(c
chan
string){
defer
close(c)
for
{
work
:=
getWork()
c
<-‐
work
}
}
func
consumer(c
chan
string)
{
for
msg
:=
range
c
{
process(msg)
}
}
func
ProducerConsumer()
{
c
:=
make(chan
string)
go
producer(c)
consumer(c)
}
32. Producer
/
Consumer
func
producer(c
chan
string){
defer
close(c)
for
{
work
:=
getWork()
c
<-‐
work
}
}
func
consumer(c
chan
string,
abort
<-‐chan
Kme.Time)
{
for
{
select
{
case
msg
:=
<-‐c:
process(msg)
case
<-‐
abort:
return
}
}
}
func
ProducerConsumer()
{
c
:=
make(chan
string)
go
producer(c)
abort
:=
Kme.A|er(2*Kme.Second)
consumer(c,
abort)
}
33. Barber
Shop
Var
seats
=
make(chan
Customer,
2)
customers
:=
[]Customer{
"Al",
"Bob",
"Chad",
"Dave"
}
)
func
barber()
{
for
{
c
:=
<-‐seats
fmt.Println("Barber
shaving",
c)
}
}
func
(c
Customer)
enter()
{
select
{
case
seats
<-‐
c:
default:
fmt.Println("Customer",
c,
"Leaves")
}
}
func
BarberShop()
{
go
barber()
for
_,
c
:=
range
customers
{
go
c.enter()
}
}