Sunteți pe pagina 1din 17

Go

Concurrent and Systems Programming

He!o World
He!o World 2.0
make

11/22/11 hello – 1
11/22/11 hello – 2
Hello Mr. 大工
package main // first program (based on doc/progs)

import "fmt"

func main() { fmt.Printf("Hello Mr. 大工\n") }

$ export GOROOT=/usr/local/go GOARCH=amd64 GOOS=darwin # CS lab Mac


$ export PATH=/usr/local/go/bin:$PATH
$ 6g hello.go
$ 6l hello.6
$ 6.out

darwin/freebsd/linux/windows, amd64/386/arm5.
main.main starts execution.
gofmt manages source formatting.

11/22/11 hello – 3
hello/hello.go

free format
but (most) line-ends act as ;
Unicode
but strings stored as UTF-8 (à la Plan9).
"string" with \ escapes,
`raw string` without.
// and /* comments.

11/22/11 hello – 4
Installation (Mac)

64-bit kernel, firewall stopped temporarily

export PATH=/bin:/usr/bin:$HOME/bin
export GOROOT= ## downloaded
export GOARCH=amd64 GOOS=darwin
cd $GOROOT/src && ./all.bash

installs commands in $HOME/bin

11/22/11 hello – 5
Standard Makefile
include $(GOROOT)/src/Make.inc

TARG=hello
GOFILES=\
hello.go\

include $(GOROOT)/src/Make.cmd

make
all% leaves target in current directory
install% puts target into $HOME/bin
clean% removes target and temporaries
nuke% also removes from $HOME/bin

11/22/11 hello – 6
Custom command makefile
cmd = hello
include ../Make.cmd
test: all ; $O/cmd makefile

include $(GOROOT)/src/Make.inc
all: $O/$(cmd)
clean: ; rm -rf [$(OS)] *.[$(OS)] ._*
doc: ; ( godoc -http=:6060 -path=.. & \
sleep 5; open -a Safari http://localhost:6060/cmd/code/$(cmd); \
sleep 15; kill $$! )
$O: ; mkdir $@
$O/%: $O %.go ; $(GC) $(GCFLAGS) -o $*.$O $*.go && \
$(LD) $(LDFLAGS) -o $@ $*.$O ../Make.cmd

make all leaves target in subdirectory


clean% removes target and temporaries
doc% formats documentation
11/22/11 hello – 7
Command documentation
/*
Hello demonstrates how to write and execute a Go program.

Usage:
hello
*/
package documentation

free-style comments immediately precede


package and top-level declarations.
command documentation in doc.go.
godoc controls formatting; use as web server.

11/22/11 hello – 8
11/22/11 hello – 9
Hello World 2.0
package main
import "fmt"
import "http"
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Hello, world")
}
func main() { // first program (according to Bob Pike)
http.HandleFunc("/", handler)
http.ListenAndServe(":8080", nil)
}

$ 6g hello2.go
$ 6l hello2.6
$ 6.out &
$ open -a Safari http://localhost:8080

11/22/11 hello – 10
hello2/hello2.go

http is a package containing a very


configurable web server.
public function names start with upper case.
HandleFunc connects a path to a handler
function which is called with a request and a
response stream.
see golang.org/pkg

11/22/11 hello – 11

http://golang.org/pkg/
make crash course
$ make name=value.. target..

reads [mM]akefile, if any, which contains a


table of targets, sources, and commands
describing a dependency tree.
sets replacement text for variables if any.
executes commands to produce targets or first
target.

11/22/11 hello – 12
# comment
name = value
..

sets replacement text for variables.

target .. : source.. ; commandline


tab commandline
tab commandline \
more of commandline
..

specifies that targets depend on sources.


specifies command lines to create targets if
they are older then the sources (if any).

11/22/11 hello – 13
Implicit rules% gmake
%: %.go ; $(GC) $(GCFLAGS) -o $*.6 $*.go && \
$(LD) $(LDFLAGS) -o $@ $*.6

% in a source is replaced by whatever % in a


target pattern matched.
$* represents the stem, i.e., whatever % in a
target pattern matched — plus the dirname
of the actual target, if any.

11/22/11 hello – 14
Variables
$n
$(name)
$(name:string=replacement)

all but single-letter names have to be in


parentheses or braces.
$@ represents the current target.
$$ represents $.

11/22/11 hello – 15
Command lines
exe:: ;@ echo starting...
exe:: ;- rm foo.bar
exe:: a.go; 6g -o a.6 a.go
exe:: b.go; 6g -o b.6 b.go
exe:: ; 6l -o exe a.6 b.6

@ command suppresses echo.


- command ignores execution error.
target:: permits several sections with
command lines.

11/22/11 hello – 16
Preprocessing% gmake
include path
ifeq (a, b)
..
else if.. ..
..
else
..
endif

include% inserts another file.


ifeq ifne% compare expanded strings.
ifdef ifndef% check if variables are defined.

11/22/11 hello – 17

S-ar putea să vă placă și