code
stringlengths
10
1.34M
language
stringclasses
1 value
// +build OMIT package main func main() { var value int // START1 OMIT // Declaring and initializing. var c chan int c = make(chan int) // or c := make(chan int) // HL // STOP1 OMIT // START2 OMIT // Sending on a channel. c <- 1 // HL // STOP2 OMIT // START3 OMIT // Receiving from a channel. // The ...
Go
// +build OMIT package main import ( "fmt" "math/rand" "time" ) type Result string type Search func(query string) Result // START1 OMIT func First(query string, replicas ...Search) Result { c := make(chan Result) searchReplica := func(i int) { c <- replicas[i](query) } for i := range replicas { go searchRep...
Go
// +build OMIT package main import ( "fmt" "math/rand" "time" ) type Result string type Search func(query string) Result var ( Web1 = fakeSearch("web1") Web2 = fakeSearch("web2") Image1 = fakeSearch("image1") Image2 = fakeSearch("image2") Video1 = fakeSearch("video1") Video2 = fakeSearch("video2")...
Go
// +build OMIT package main import ( "fmt" "math/rand" "time" ) type Result string type Search func(query string) Result var ( Web = fakeSearch("web") Image = fakeSearch("image") Video = fakeSearch("video") ) func Google(query string) (results []Result) { c := make(chan Result) go func() { c <- Web(query)...
Go
// +build OMIT package main import ( "fmt" "time" ) func main() { boring("boring!") } // START OMIT func boring(msg string) { for i := 0; ; i++ { fmt.Println(msg, i) time.Sleep(time.Second) } } // STOP OMIT
Go
// +build OMIT package main import ( "fmt" "math/rand" "time" ) func main() { go boring("boring!") fmt.Println("I'm listening.") time.Sleep(2 * time.Second) fmt.Println("You're boring; I'm leaving.") } // STOP OMIT func boring(msg string) { for i := 0; ; i++ { fmt.Println(msg, i) time.Sleep(time.Duratio...
Go
// +build OMIT package main import ( "fmt" "math/rand" "time" ) func main() { boring("boring!") } // STOP OMIT func boring(msg string) { for i := 0; ; i++ { fmt.Println(msg, i) time.Sleep(time.Duration(rand.Intn(1e3)) * time.Millisecond) } }
Go
// +build OMIT package main import ( "fmt" "math/rand" "time" ) func main() { // START1 OMIT quit := make(chan bool) // HL c := boring("Joe", quit) for i := rand.Intn(10); i >= 0; i-- { fmt.Println(<-c) } quit <- true // HL // STOP1 OMIT } func boring(msg string, quit <-chan bool) <-chan string { c := make(...
Go
// +build OMIT package main import ( "fmt" "math/rand" "time" ) type Result string // START1 OMIT func Google(query string) (results []Result) { results = append(results, Web(query)) results = append(results, Image(query)) results = append(results, Video(query)) return } // STOP1 OMIT // START2 OMIT var ( ...
Go
// +build OMIT package main import ( "fmt" "io" "log" "net" ) const listenAddr = "localhost:4000" func main() { l, err := net.Listen("tcp", listenAddr) if err != nil { log.Fatal(err) } for { c, err := l.Accept() if err != nil { log.Fatal(err) } go match(c) } } var partner = make(chan io.ReadW...
Go
// +build OMIT package main import ( "fmt" "math/rand" "time" ) func main() { go boring("boring!") // HL } // STOP OMIT func boring(msg string) { for i := 0; ; i++ { fmt.Println(msg, i) time.Sleep(time.Duration(rand.Intn(1e3)) * time.Millisecond) } }
Go
// +build OMIT package main import "fmt" func f(left, right chan int) { left <- 1 + <-right } func main() { const n = 10000 leftmost := make(chan int) right := leftmost left := leftmost for i := 0; i < n; i++ { right = make(chan int) go f(left, right) left = right } go func(c chan int) { c <- 1 }(righ...
Go
// +build OMIT package main import ( "fmt" "math/rand" "time" ) // START1 OMIT func main() { joe := boring("Joe") // HL ann := boring("Ann") // HL for i := 0; i < 5; i++ { fmt.Println(<-joe) fmt.Println(<-ann) } fmt.Println("You're both boring; I'm leaving.") } // STOP1 OMIT // START2 OMIT func boring(m...
Go
// +build OMIT package main import ( "fmt" "math/rand" "time" ) func main() { boring("boring!") // HL } // START OMIT func boring(msg string) { for i := 0; ; i++ { fmt.Println(msg, i) time.Sleep(time.Duration(rand.Intn(1e3)) * time.Millisecond) } } // STOP OMIT
Go
// +build OMIT package main import ( "fmt" "math/rand" "time" ) // START0 OMIT type Message struct { str string wait chan bool // HL } // STOP0 OMIT func main() { c := fanIn(boring("Joe"), boring("Ann")) // HL // START1 OMIT for i := 0; i < 5; i++ { msg1 := <-c; fmt.Println(msg1.str) msg2 := <-c; fmt.Pri...
Go
// +build OMIT package main import ( "fmt" "math/rand" "time" ) // START1 OMIT func main() { c := boring("Joe") timeout := time.After(5 * time.Second) // HL for { select { case s := <-c: fmt.Println(s) case <-timeout: // HL fmt.Println("You talk too much.") return } } } // STOP1 OMIT // STAR...
Go
// +build OMIT package main import ( "fmt" "math/rand" "time" ) // START1 OMIT func main() { c := boring("Joe") for { select { case s := <-c: fmt.Println(s) case <-time.After(1 * time.Second): // HL fmt.Println("You're too slow.") return } } } // STOP1 OMIT // START2 OMIT func boring(msg stri...
Go
// +build OMIT package main import ( "fmt" "math/rand" "time" ) type Result string type Search func(query string) Result var ( Web = fakeSearch("web") Image = fakeSearch("image") Video = fakeSearch("video") ) func Google(query string) (results []Result) { // START OMIT c := make(chan Result) go func() { c ...
Go
// +build OMIT package main import ( "fmt" "math/rand" "time" ) // START1 OMIT func main() { c := fanIn(boring("Joe"), boring("Ann")) // HL for i := 0; i < 10; i++ { fmt.Println(<-c) // HL } fmt.Println("You're both boring; I'm leaving.") } // STOP1 OMIT // START2 OMIT func boring(msg string) <-chan string...
Go
// +build OMIT package main import ( "fmt" "math/rand" "time" ) // START1 OMIT func main() { c := fanIn(boring("Joe"), boring("Ann")) // HL for i := 0; i < 10; i++ { fmt.Println(<-c) // HL } fmt.Println("You're both boring; I'm leaving.") } // STOP1 OMIT // START2 OMIT func boring(msg string) <-chan string...
Go
// +build OMIT package main import ( "fmt" "math/rand" "time" ) // START1 OMIT func main() { c := make(chan string) go boring("boring!", c) for i := 0; i < 5; i++ { fmt.Printf("You say: %q\n", <-c) // Receive expression is just a value. // HL } fmt.Println("You're boring; I'm leaving.") } // STOP1 OMIT //...
Go
// +build OMIT package main import ( "fmt" "math/rand" "time" ) func cleanup() { } func main() { // START1 OMIT quit := make(chan string) // HL c := boring("Joe", quit) // HL for i := rand.Intn(10); i >= 0; i-- { fmt.Println(<-c) } quit <- "Bye!" // HL fmt.Printf("Joe says: %q\n", <-quit) // HL // STOP1 OMI...
Go
// +build OMIT package main import ( "fmt" "math/rand" "time" ) func main() { // START1 OMIT c := boring("boring!") // Function returning a channel. // HL for i := 0; i < 5; i++ { fmt.Printf("You say: %q\n", <-c) } fmt.Println("You're boring; I'm leaving.") // STOP1 OMIT } // START2 OMIT func boring(msg st...
Go
// +build OMIT package main func main() { var c1, c2, c3 chan int // START0 OMIT select { case v1 := <-c1: fmt.Printf("received %v from c1\n", v1) case v2 := <-c2: fmt.Printf("received %v from c2\n", v1) case c3 <- 23: fmt.Printf("sent %v to c3\n", 23) default: fmt.Printf("no one was ready to communica...
Go
// +build OMIT package main import ( "code.google.com/p/go.net/websocket" "fmt" "net/http" ) func main() { http.Handle("/", websocket.Handler(handler)) http.ListenAndServe("localhost:4000", nil) } func handler(c *websocket.Conn) { var s string fmt.Fscan(c, &s) fmt.Println("Received:", s) fmt.Fprint(c, "How...
Go
// +build OMIT package main import ( "fmt"; "time" ) func main() { for { fmt.Println("Hello, Gophers!") time.Sleep(time.Second) } }
Go
// Copyright 2012 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // +build OMIT package main import ( "encoding/json" "io/ioutil" "log" "net/http" "os" "os/exec" "path/filepath" "runtime" "strconv" "code.google....
Go
// Copyright 2012 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // +build OMIT package main import ( "encoding/json" "io/ioutil" "log" "net/http" "os" "os/exec" "path/filepath" "runtime" "strconv" "code.google....
Go
// +build OMIT package main import ( "fmt"; "net/http"; "time" ) func main() { urls := []string{"http://google.com/", "http://bing.com/"} start := time.Now() done := make(chan string) for _, u := range urls { go func(u string) { resp, err := http.Get(u) if err != nil { done <- u + " " + err.Error() ...
Go
// +build OMIT package main import ( "encoding/json"; "fmt"; "io"; "os" ) func main() { d := json.NewDecoder(os.Stdin) var err error for err == nil { var v interface{} if err = d.Decode(&v); err != nil { break } var b []byte if b, err = json.MarshalIndent(v, "", " "); err != nil { break } _, ...
Go
// +build OMIT package main import "fmt" func main() { fmt.Println("Hello, Pythonistas!") }
Go
// +build OMIT package main import ( "fmt" "net/http" ) func main() { http.HandleFunc("/", hello) http.ListenAndServe("localhost:8000", nil) } func hello(w http.ResponseWriter, r *http.Request) { fmt.Fprintln(w, "Hello, Pythonistas!") }
Go
// +build OMIT package main import "fmt" var primes = [...]int{2, 3, 5, 7, 11, 13, 17, 19} func _() { // START1 OMIT for i := 0; i < len(primes); i++ { fmt.Println(i, primes[i]) } // STOP1 OMIT // START2 OMIT var sum int for _, x := range primes { sum += x } // STOP2 OMIT } func main() { // START3 O...
Go
// +build OMIT package main import "fmt" type Point struct{ x, y int } func PointToString(p Point) string { return fmt.Sprintf("Point{%d, %d}", p.x, p.y) } func (p Point) String() string { // HL return fmt.Sprintf("Point{%d, %d}", p.x, p.y) } func main() { p := Point{3, 5} fmt.Println(PointToString(p)) // sta...
Go
// +build OMIT package main import ( "fmt" "time" ) // START OMIT func main() { go f("three", 300*time.Millisecond) go f("six", 600*time.Millisecond) go f("nine", 900*time.Millisecond) time.Sleep(3 * time.Second) fmt.Println("Done.") } // STOP OMIT func f(msg string, delay time.Duration) { for i := 0; ; i+...
Go
// +build OMIT package main import ( "flag" "io" "os" ) func main() { flag.Parse() for _, arg := range flag.Args() { f, err := os.Open(arg) if err != nil { panic(err) } defer f.Close() _, err = io.Copy(os.Stdout, f) // HL if err != nil { panic(err) } } }
Go
// +build OMIT package main import ( "fmt" "time" ) // START1 OMIT func main() { c := make(chan string) go f("three", 300*time.Millisecond, c) for i := 0; i < 10; i++ { fmt.Println("Received", <-c) // Receive expression is just a value. // HL } fmt.Println("Done.") } // STOP1 OMIT // START2 OMIT func f(ms...
Go
// +build OMIT package main func main() { var value int // START1 OMIT // Declaring and initializing. var c chan int c = make(chan int) // or c := make(chan int) // HL // STOP1 OMIT // START2 OMIT // Sending on a channel. c <- 1 // HL // STOP2 OMIT // START3 OMIT // Receiving from a channel. // The ...
Go
// +build OMIT package main import "fmt" type Point struct{ x, y int } func (p Point) String() string { return fmt.Sprintf("Point{%d, %d}", p.x, p.y) } type Celsius float32 type Fahrenheit float32 func (t Celsius) String() string { return fmt.Sprintf("%g°C", t) } func (t Fahrenheit) String() string ...
Go
// +build OMIT package main import ( "fmt" "time" ) // START1 OMIT func main() { c := make(chan string) go f("three", 300*time.Millisecond, c) // HL go f("six", 600*time.Millisecond, c) // HL go f("nine", 900*time.Millisecond, c) // HL for i := 0; i < 10; i++ { fmt.Println("Received", <-c) } fmt.Printl...
Go
// +build OMIT package main import ( "fmt" "time" ) // START OMIT func main() { go f("three", 300*time.Millisecond) go f("six", 600*time.Millisecond) go f("nine", 900*time.Millisecond) } // STOP OMIT func f(msg string, delay time.Duration) { for i := 0; ; i++ { fmt.Println(msg, i) time.Sleep(delay) } }
Go
// +build OMIT package main // START1 OMIT var i int var p, q *Point var threshold float64 = 0.75 // STOP1 OMIT // START2 OMIT var i = 42 // type of i is int var z = 1 + 2.3i // type of z is complex128 // STOP2 OMIT func _() int { i := 42 // type of i is int return &i }
Go
// +build OMIT package main import "fmt" import "time" // START1 OMIT func main() { start := time.Now() in := make(chan int) // Channel on which work orders are received. out := make(chan []int) // Channel on which results are returned. go producer(in) go worker(in, out) // Launch one worker. // HL consume...
Go
// +build OMIT package main import "fmt" func main() { fmt.Println("Hello, 世界") }
Go
// +build OMIT package main import "fmt" const digits = "0123456789abcdef" func itoa(x, base int) string { // START OMIT t := x switch { case x == 0: return "0" case x < 0: t = -x } var s [32]byte i := len(s) for t != 0 { // Look, ma, no ()'s! i-- s[i] = digits[t%base] t /= base } if x < 0 { ...
Go
// +build OMIT package main import ( "fmt" "time" ) // START1 OMIT func main() { f("Hello, World", 500*time.Millisecond) } // STOP1 OMIT // START2 OMIT func f(msg string, delay time.Duration) { for i := 0; ; i++ { fmt.Println(msg, i) time.Sleep(delay) } } // STOP2 OMIT
Go
// +build OMIT package main import "fmt" type Celsius float32 type Fahrenheit float32 func (t Celsius) String() string { return fmt.Sprintf("%g°C", t) } func (t Fahrenheit) String() string { return fmt.Sprintf("%g°F", t) } func (t Celsius) ToFahrenheit() Fahrenheit { return Fahrenheit(t*9/5 + 32) }...
Go
// +build OMIT package main import "fmt" import "time" func main() { start := time.Now() // START1 OMIT in := make(chan int) out := make(chan []int) go producer(in) // Launch 10 workers. // HL for i := 0; i < 10; i++ { // HL go worker(in, out) // HL } // HL consumer(out, 100) // STOP1 OMIT fmt.Println...
Go
// +build OMIT package main import "fmt" // START1 OMIT func adder(delta int) func(x int) int { f := func(x int) int { // HL return x + delta // HL } // HL return f } // STOP1 OMIT func main() { // START2 OMIT var inc = adder(1) fmt.Println(inc(0)) fmt.Println(adder(-1)(10)) // STOP2 OMIT }
Go
// +build OMIT package main import "fmt" // START1 OMIT const ( MaxUInt = 1<<64 - 1 Pi = 3.14159265358979323846264338327950288419716939937510582097494459 Pi2 = Pi * Pi Delta = 2.0 ) // STOP1 OMIT func main() { // START2 OMIT var x uint64 = MaxUInt var pi2 float32 = Pi2 var delta int = Delta // ...
Go
// +build OMIT package main // START OMIT import "fmt" const digits = "0123456789abcdef" type Point struct { x, y int tag string } var s [32]byte var msgs = []string{"Hello, 世界", "Ciao, Mondo"} func itoa(x, base int) string // STOP OMIT func main() { fmt.Println() // use fmt }
Go
// +build OMIT package io type Writer interface { Write(p []byte) (n int, err error) } type Reader interface { Read(p []byte) (n int, err error) }
Go
// +build OMIT package main import ( "fmt" "time" ) func main() { // START OMIT if time.Now().Hour() < 12 { fmt.Println("Good morning.") } else { fmt.Println("Good afternoon (or evening).") } // END OMIT }
Go
// +build OMIT package main import "strings" import "testing" func TestToUpper(t *testing.T) { in := "loud noises" want := "LOUD NOISES" got := strings.ToUpper(in) if got != want { t.Errorf("ToUpper(%v) = %v, want %v", in, got, want) } } func TestContains(t *testing.T) { var tests = []struct { str, subst...
Go
// +build OMIT package main import ( "fmt" "time" ) func main() { // START OMIT birthday, _ := time.Parse("Jan 2 2006", "Nov 10 2009") // time.Time age := time.Since(birthday) // time.Duration fmt.Printf("Go is %d days old\n", age/(time.Hour*24)) // END OMIT }
Go
// +build OMIT // This is a somewhat cut back version of webfront, available at // http://github.com/nf/webfront /* Copyright 2011 Google Inc. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at ...
Go
// +build OMIT package main import ( "fmt" "time" ) func main() { // START OMIT t := time.Now() fmt.Println(t.In(time.UTC)) home, _ := time.LoadLocation("Australia/Sydney") fmt.Println(t.In(home)) // END OMIT }
Go
// +build OMIT package main import "fmt" func main() { fmt.Println("Hello, go") }
Go
// +build OMIT package main import ( "flag" "fmt" "time" ) var ( message = flag.String("message", "Hello!", "what to say") delay = flag.Duration("delay", 2*time.Second, "how long to wait") ) func main() { flag.Parse() fmt.Println(*message) time.Sleep(*delay) }
Go
// +build OMIT package main import ( "compress/gzip" "encoding/base64" "io" "os" "strings" ) func main() { var r io.Reader r = strings.NewReader(data) r = base64.NewDecoder(base64.StdEncoding, r) r, _ = gzip.NewReader(r) io.Copy(os.Stdout, r) } const data = ` H4sIAAAJbogA/1SOO5KDQAxE8zlFZ5tQXGCjjfYIjoURoP...
Go
// +build OMIT package main import ( "encoding/json" "fmt" "strings" ) const blob = `[ {"Title":"Øredev", "URL":"http://oredev.org"}, {"Title":"Strange Loop", "URL":"http://thestrangeloop.com"} ]` type Item struct { Title string URL string } func main() { var items []*Item json.NewDecoder(strings.NewRea...
Go
// +build OMIT package main import ( "fmt" "log" "net/http" ) type Greeting string func (g Greeting) ServeHTTP(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, g) } func main() { err := http.ListenAndServe("localhost:4000", Greeting("Hello, go")) if err != nil { log.Fatal(err) } }
Go
// +build OMIT package main import ( "container/heap" "fmt" "math/rand" "time" ) const nRequester = 100 const nWorker = 10 // Simulation of some work: just sleep for a while and report how long. func op() int { n := rand.Int63n(int64(time.Second)) time.Sleep(time.Duration(nWorker * n)) return int(n) } type ...
Go
// +build OMIT package main import ( "fmt" "math/rand" "time" ) // START1 OMIT func main() { c := fanIn(boring("Joe"), boring("Ann")) // HL for i := 0; i < 10; i++ { fmt.Println(<-c) // HL } fmt.Println("You're both boring; I'm leaving.") } // STOP1 OMIT // START2 OMIT func boring(msg string) <-chan string...
Go
// +build ignore package main import ( "fmt" "go/ast" "go/parser" "go/token" ) func main() { src := ` package http type Handler interface { ServeHTTP(ResponseWriter, *Request) } ` f, _ := parser.ParseFile(token.NewFileSet(), "", src, 0) typ := f.Decls[0].(*ast.GenDecl).Specs[0].(*ast.TypeSpec).Type.(*ast.Int...
Go
// +build ignore package main import ( "fmt" "go/parser" "go/token" "strconv" ) func main() { src := `package hack; import "net/http"; var i http.Handler` fset := token.NewFileSet() f, _ := parser.ParseFile(fset, "", src, 0) raw := f.Imports[0].Path.Value path, _ := strconv.Unquote(raw) fmt.Println(raw, ...
Go
// +build ignore package main import ( "fmt" "code.google.com/p/go.tools/imports" ) func main() { iface := "http.Handler" src := "package hack; var i " + iface // HL fmt.Println(src, "\n---") imp, _ := imports.Process("", []byte(src), nil) // HL // ignoring errors throughout this presentation fmt.Println(s...
Go
// +build ignore package main import ( "fmt" "go/build" ) func main() { pkg, _ := build.Import("net/http", "", 0) // HL fmt.Println(pkg.Dir) fmt.Println(pkg.GoFiles) }
Go
// +build ignore package main import ( "fmt" "go/format" ) func main() { ugly := `func (f *File) Read(p []byte, )(n int, err error, ){}` fmt.Println(ugly) pretty, _ := format.Source([]byte(ugly)) // HL fmt.Println(string(pretty)) }
Go
// +build ignore package main import ( "fmt" "go/ast" "go/parser" "go/token" ) func main() { src := `package hack; import "net/http"; var i http.Handler` f, _ := parser.ParseFile(token.NewFileSet(), "", src, 0) decl := f.Decls[1].(*ast.GenDecl) // var i http.Handler spec := decl.Specs[0].(*ast.ValueSpe...
Go
// +build ignore package main import ( "go/ast" "go/build" "go/parser" "go/printer" "go/token" "os" "path/filepath" ) func main() { fset, files := parsePackage("net/http") id := "Handler" for _, f := range files { for _, decl := range f.Decls { decl, ok := decl.(*ast.GenDecl) if !ok || decl.Tok !=...
Go
// +build ignore package main // Method represents a method signature. type Method struct { Recv string Func } // Func represents a function signature. type Func struct { Name string Params []Param Res []Param } // Param represents a parameter in a function or method signature. type Param struct { Name s...
Go
// +build ignore package main import ( "os" "text/template" ) func main() { const stub = "func ({{.Recv}}) {{.Name}}" + "({{range .Params}}{{.Name}} {{.Type}}, {{end}})" + "({{range .Res}}{{.Name}} {{.Type}}, {{end}})" + "{\n}\n\n" tmpl := template.Must(template.New("test").Parse(stub)) m := Method{ Re...
Go
// +build OMIT package main import ( "fmt" "go/ast" "go/parser" "go/token" "io/ioutil" "path/filepath" "runtime" "strings" ) func walk(dir string, f func(string) bool) bool { fis, err := ioutil.ReadDir(dir) if err != nil { panic(err) } // parse all *.go files in directory; // traverse subdirectories, ...
Go
// +build OMIT package main import ( "fmt" "go/ast" "go/parser" "go/token" "io/ioutil" "path/filepath" "runtime" "sort" "strings" "time" ) func walk(dir string, f func(string) bool) bool { fis, err := ioutil.ReadDir(dir) if err != nil { panic(err) } // parse all *.go files in directory; // traverse ...
Go
// +build OMIT package main import "fmt" // Point START OMIT type Point struct { x, y int } // Point END OMIT // String START OMIT func (p Point) String() string { return fmt.Sprintf("(%d, %d)", p.x, p.y) } // String END OMIT // main START OMIT func main() { p := Point{2, 3} fmt.Println(p.String()) fmt.Prin...
Go
// +build OMIT package main import ( "fmt" "sort" ) type Weekday int const ( Mon Weekday = iota Tue Wed Thu Fri Sat Sun ) var names = [...]string{"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"} func (d Weekday) String() string { // ... return names[d] } // lexical START OM...
Go
// +build OMIT package main import "fmt" // type START OMIT type Weekday int // type END OMIT const ( Mon Weekday = iota Tue Wed Thu Fri Sat Sun ) var names = [...]string{"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"} // String START OMIT func (d Weekday) String() string { /...
Go
// +build OMIT package main import "fmt" type Point struct { x, y int } func (p Point) String() string { return fmt.Sprintf("(%d, %d)", p.x, p.y) } type Weekday int const ( Mon Weekday = iota Tue Wed Thu Fri Sat Sun ) var names = [...]string{"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Satur...
Go
// +build OMIT package main // idents.go import ( "fmt" "os" "text/scanner" ) func main() { var s scanner.Scanner s.Init(os.Stdin) for { switch s.Scan() { case scanner.EOF: return // all done case scanner.Ident: fmt.Println(s.TokenText()) } } }
Go
// +build OMIT package main import ( "fmt" "time" ) // f START OMIT func f(msg string, delay time.Duration, ch chan string) { for { ch <- msg time.Sleep(delay) } } // f END OMIT // main START OMIT func main() { ch := make(chan string) go f("A--", 300*time.Millisecond, ch) go f("-B-", 500*time.Millisecon...
Go
// +build OMIT package main import "fmt" func main() { fmt.Println("Hello, 世界!") }
Go
package main import ( "fmt" "log" "net/http" ) func HelloServer(w http.ResponseWriter, req *http.Request) { log.Println(req.URL) fmt.Fprintf(w, "Hello, 世界!\nURL = %s\n", req.URL) } func main() { fmt.Println("please connect to localhost:7777/hello") http.HandleFunc("/hello", HelloServer) log.Fatal(http.Listen...
Go
// +build OMIT package main import ( "fmt" "time" ) // f START OMIT func f(msg string, delay time.Duration) { for { fmt.Println(msg) time.Sleep(delay) } } // f END OMIT // main START OMIT func main() { go f("A--", 300*time.Millisecond) go f("-B-", 500*time.Millisecond) go f("--C", 1100*time.Millisecond)...
Go
// +build OMIT package main import ( "fmt" "go/ast" "go/parser" "go/token" "io/ioutil" "path/filepath" "runtime" "sort" "strings" "time" ) func walk(dir string, f func(string) bool) bool { fis, err := ioutil.ReadDir(dir) if err != nil { panic(err) } // parse all *.go files in directory; // traverse ...
Go
// +build OMIT package main import ( "fmt" "io/ioutil" "path/filepath" "runtime" "strings" ) func walk(dir string, f func(string) bool) bool { fis, err := ioutil.ReadDir(dir) if err != nil { panic(err) } // parse all *.go files in directory; // traverse subdirectories, but don't walk into testdata for _...
Go
// +build OMIT package examples // IndexOfAny START OMIT func IndexOfAny(str string, chars []rune) int { if len(str) == 0 || len(chars) == 0 { return -1 } for i, ch := range str { for _, match := range chars { if ch == match { return i } } } return -1 } // IndexOfAny END OMIT
Go
// +build ignore,OMIT package main func main() int { print "hello, world\n"; return 0; }
Go
// +build ignore,OMIT package main // Send the sequence 2, 3, 4, ... to channel 'ch'. func Generate(ch chan <- int) { for i := 2; ; i++ { ch <- i // Send 'i' to channel 'ch'. } } // Copy the values from channel 'in' to channel 'out', // removing those divisible by 'prime'. func Filter(in chan <- int, out *<-cha...
Go
// +build ignore,OMIT package main import "fmt" func main() { fmt.Printf("hello, world\n") }
Go
// +build ignore,OMIT package Main // Send the sequence 2, 3, 4, ... to channel 'ch'. func Generate(ch *chan> int) { for i := 2; ; i++ { >ch = i; // Send 'i' to channel 'ch'. } } // Copy the values from channel 'in' to channel 'out', // removing those divisible by 'prime'. func Filter(in *chan< int, out *chan> i...
Go
// +build ignore,OMIT package main func main() { print "hello, world\n"; }
Go
// +build ignore,OMIT package main func main() { print("hello, world\n"); }
Go
// +build ignore,OMIT package main import "fmt" // Send the sequence 2, 3, 4, ... to channel 'ch'. func generate(ch chan<- int) { for i := 2; ; i++ { ch <- i; // Send 'i' to channel 'ch'. } } // Copy the values from channel 'in' to channel 'out', // removing those divisible by 'prime'. func filter(src <-chan in...
Go
// +build ignore,OMIT package main import "fmt" func main() { fmt.Printf("Hello, gophers!\n") }
Go
// +build ignore,OMIT package main import "fmt" func main() { fmt.printf("hello, world\n"); }
Go
// +build ignore,OMIT package main import "fmt" // Send the sequence 2, 3, 4, … to channel 'ch'. func generate(ch chan<- int) { for i := 2; ; i++ { ch <- i // Send 'i' to channel 'ch'. } } // Copy the values from channel 'src' to channel 'dst', // removing those divisible by 'prime'. func filter(src <-chan int...
Go
// +build ignore,OMIT package main import "fmt" func main() { fmt.Println("Hello, Gophers (some of whom know 日本語)!") }
Go
// +build ignore,OMIT package main // Send the sequence 2, 3, 4, ... to channel 'ch'. func Generate(ch *chan-< int) { for i := 2; ; i++ { ch -< i // Send 'i' to channel 'ch'. } } // Copy the values from channel 'in' to channel 'out', // removing those divisible by 'prime'. func Filter(in *chan<- int, out *chan-<...
Go
// +build ignore,OMIT package main import "fmt" func main() { fmt.Printf("hello, world\n"); }
Go