Documente Academic
Documente Profesional
Documente Cultură
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"
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)
export PATH=/bin:/usr/bin:$HOME/bin
export GOROOT= ## downloaded
export GOARCH=amd64 GOOS=darwin
cd $GOROOT/src && ./all.bash
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
Usage:
hello
*/
package documentation
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
11/22/11 hello – 11
http://golang.org/pkg/
make crash course
$ make name=value.. target..
11/22/11 hello – 12
# comment
name = value
..
11/22/11 hello – 13
Implicit rules% gmake
%: %.go ; $(GC) $(GCFLAGS) -o $*.6 $*.go && \
$(LD) $(LDFLAGS) -o $@ $*.6
11/22/11 hello – 14
Variables
$n
$(name)
$(name:string=replacement)
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
11/22/11 hello – 16
Preprocessing% gmake
include path
ifeq (a, b)
..
else if.. ..
..
else
..
endif
11/22/11 hello – 17