code
stringlengths
10
1.34M
language
stringclasses
1 value
// +build ignore package main import "fmt" // 2 START OMIT // 1 START OMIT func main() { i := 1 f := func() T { return T{ i: 1, // HL } } fmt.Println(i, f()) } // 1 END OMIT type T struct{ i int } // 2 END OMIT
Go
// +build ignore package main import "fmt" // 1 START OMIT const C1 = 1e+308 const C2 = C1 * 10 const C3 = C2 / 10 var V1 = C1 var V2 = V1 * 10 var V3 = V2 / 10 func main() { fmt.Println(C3, V3) } // 1 END OMIT
Go
// +build ignore package main import ( "fmt" "runtime" ) // 1 START OMIT type P *P type S []S type C chan C type M map[int]M // 1 END OMIT // 2 START OMIT func Val(p *P) int { if p == nil { return 0 } else { return 1 + Val(*p) } } func Add(a, b *P) *P { if b == nil { return a } else { a1 := new(P) ...
Go
// +build ignore package main import "fmt" // 1 START OMIT type F func(*State) F type State int func Begin(s *State) F { *s = 1 return Middle } func Middle(s *State) F { *s++ if *s >= 10 { return End } return Middle } // 1 END OMIT // 2 START OMIT func End(s *State) F { fmt.Println(*s) return nil } fu...
Go
// +build ignore package main import ( "fmt" "unsafe" ) // 1 START OMIT var V1 = 0x01020304 var V2 [unsafe.Sizeof(V1)]byte func main() { *(*int)(unsafe.Pointer(&V2)) = V1 fmt.Println(V2) } // 1 END OMIT
Go
// +build ignore package main import "fmt" // 2 START OMIT // 1 START OMIT func main() { i := 1 f := func() T { return T{ i: 1, // HL } } fmt.Println(i, f()) } // 1 END OMIT type T map[int]int // 2 END OMIT
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" "time" "math/rand" ) func waiter(i int, block, done chan struct{}) { time.Sleep(time.Duration(rand.Intn(3000)) * time.Millisecond) fmt.Println(i, "waiting...") <-block // HL fmt.Println(i, "done!") done <- struct{}{} } func main() { block, done := make(chan struct...
Go
// +build OMIT package main import ( "fmt" "math/rand" "time" ) func worker(i int, ch chan Work, quit chan struct{}) { for { select { case w := <-ch: if quit == nil { // HL w.Refuse(); fmt.Println("worker", i, "refused", w) break } w.Do(); fmt.Println("worker", i, "processed", w) case <-qu...
Go
// +build OMIT package main import "fmt" var battle = make(chan string) func warrior(name string, done chan struct{}) { select { case opponent := <-battle: fmt.Printf("%s beat %s\n", name, opponent) case battle <- name: // I lost :-( } done <- struct{}{} } func main() { done := make(chan struct{}) langs...
Go
// +build OMIT package main import ( "fmt" "math/rand" "time" ) func worker(i int, ch chan Work, quit chan struct{}) { var quitting bool for { select { case w := <-ch: if quitting { w.Refuse(); fmt.Println("worker", i, "refused", w) break } w.Do(); fmt.Println("worker", i, "processed", w) ...
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" "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 ( "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 ( "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 // 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 "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" "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 "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 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 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 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" ) 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 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 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" ) // 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" ) 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" ) // 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" ) // 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" 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() { 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 := 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" ) 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" "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 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 ( "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" ) // 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" ) 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 ( 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" "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 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 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" "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!") // 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" ) 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 ( "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 // 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 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" // 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" "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 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" 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" 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" "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 "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" "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 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" 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() { 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" func main() { fmt.Println("Hello, 世界") }
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 // 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" 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 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" "github.com/nf/reddit" // HL "log" ) func main() { items, err := reddit.Get("golang") // HL if err != nil { log.Fatal(err) } for _, item := range items { fmt.Println(item) } }
Go
// Package reddit implements a basic client for the Reddit API. // +build OMIT package reddit import ( "encoding/json" "fmt" "net/http" ) // Item describes a Reddit item. type Item struct { Title string URL string Comments int `json:"num_comments"` } func (i Item) String() string { com := "" switch ...
Go
// +build OMIT package main import ( "encoding/json" "errors" "fmt" "log" "net/http" ) func main() { items, err := Get("golang") // HL if err != nil { log.Fatal(err) } for _, item := range items { // HL fmt.Println(item.Title) } } type Response struct { Data struct { Children []struct { Data Ite...
Go
// +build OMIT package main import ( "encoding/json" "errors" "fmt" "log" "net/http" ) func main() { items, err := Get("golang") if err != nil { log.Fatal(err) } for _, item := range items { fmt.Println(item) } } type Response struct { Data struct { Children []struct { Data Item } } } type I...
Go
// +build OMIT package main import ( "log" "net/http" ) func main() { b := []byte(jsonBlob) err := http.ListenAndServe(":80", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Write(b) })) log.Fatal(err) } const jsonBlob = `{"data":{"after":"t3_ubvcv","before":null,"children":[{"data":{"appr...
Go
// +build OMIT package main import ( "io" "log" "net/http" "os" ) func main() { // HLfunc resp, err := http.Get("http://reddit.com/r/golang.json") // HLget if err != nil { // HLerr log.Fatal(err) // HLerr } // HLerr if resp.StatusCode != http.StatusOK { // HLstatus ...
Go
// +build OMIT package main import "fmt" func main() { fmt.Println("Greetings, fellow gopher") }
Go
// +build OMIT package main import ( "encoding/json" "fmt" "log" "net/http" ) func main() { resp, err := http.Get("http://reddit.com/r/golang.json") if err != nil { log.Fatal(err) } if resp.StatusCode != http.StatusOK { log.Fatal(resp.Status) } r := new(Response) err = json.NewDecoder(resp.Body).Decod...
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"; "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 "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
// 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 ( "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
// 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"; "time" ) func main() { for { fmt.Println("Hello, Gophers!") time.Sleep(time.Second) } }
Go
// +build OMIT package main import "html/template" import "net/http" func rootHandler(w http.ResponseWriter, r *http.Request) { rootTemplate.Execute(w, listenAddr) } var rootTemplate = template.Must(template.New("root").Parse(` <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <script> var input, output, web...
Go
// +build OMIT package main import ( "fmt" "io" "log" "net/http" "code.google.com/p/go.net/websocket" ) const listenAddr = "localhost:4000" func main() { http.HandleFunc("/", rootHandler) http.Handle("/socket", websocket.Handler(socketHandler)) err := http.ListenAndServe(listenAddr, nil) if err != nil { ...
Go
// +build OMIT package main import "html/template" import "net/http" func rootHandler(w http.ResponseWriter, r *http.Request) { rootTemplate.Execute(w, listenAddr) } var rootTemplate = template.Must(template.New("root").Parse(` <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <script> var input, output, web...
Go
// +build OMIT package main // This Markov chain code is taken from the "Generating arbitrary text" // codewalk: http://golang.org/doc/codewalk/markov/ import ( "bytes" "fmt" "math/rand" "strings" "sync" ) // Prefix is a Markov chain prefix of one or more words. type Prefix []string // String returns the Pref...
Go
// +build OMIT package main import ( "fmt" "io" "log" "net/http" "time" "code.google.com/p/go.net/websocket" ) const listenAddr = "localhost:4000" func main() { http.HandleFunc("/", rootHandler) http.Handle("/socket", websocket.Handler(socketHandler)) err := http.ListenAndServe(listenAddr, nil) if err !=...
Go
// +build OMIT package main import ( "fmt" "time" ) func main() { go say("let's go!", 3) go say("ho!", 2) go say("hey!", 1) time.Sleep(4 * time.Second) } func say(text string, secs int) { time.Sleep(time.Duration(secs) * time.Second) fmt.Println(text) }
Go
// +build OMIT package main import ( "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) } io.Copy(c, c) } }
Go
// +build OMIT package main import ( "fmt" "time" ) func main() { ticker := time.NewTicker(time.Millisecond * 250) boom := time.After(time.Second * 1) for { select { case <-ticker.C: fmt.Println("tick") case <-boom: fmt.Println("boom!") return } } }
Go
// +build OMIT package main import "fmt" type A struct{} func (A) Hello() { fmt.Println("Hello!") } type B struct { A } // func (b B) Hello() { b.A.Hello() } // (implicitly!) func main() { var b B b.Hello() }
Go
// +build OMIT package main func Fprintln(w io.Writer, a ...interface{}) (n int, err error) // Writer is the interface that wraps the basic Write method. // // Write writes len(p) bytes from p to the underlying data stream. It // returns the number of bytes written from p (0 <= n <= len(p)) and any // error encounte...
Go
// +build OMIT package main import ( "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 io.Copy(c, c) } }
Go
// +build OMIT package main import ( "fmt" "log" "net/http" ) const listenAddr = "localhost:4000" func main() { http.HandleFunc("/", handler) err := http.ListenAndServe(listenAddr, nil) if err != nil { log.Fatal(err) } } func handler(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, "Hello, web") }...
Go
// +build OMIT package main import "fmt" func main() { ch := make(chan int) go fibs(ch) for i := 0; i < 20; i++ { fmt.Println(<-ch) } } func fibs(ch chan int) { i, j := 0, 1 for { ch <- j i, j = j, i+j } }
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" func main() { fmt.Println("Hello, go") }
Go
// +build OMIT package main import ( "fmt" "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) } fmt.Fprintln(c, "Hello!") c.Close() } }
Go
// +build OMIT package main import "html/template" import "net/http" func rootHandler(w http.ResponseWriter, r *http.Request) { rootTemplate.Execute(w, listenAddr) } var rootTemplate = template.Must(template.New("root").Parse(` <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <script> var input, output, web...
Go
// +build OMIT package main import ( "fmt" "io" "log" "net/http" "code.google.com/p/go.net/websocket" ) const listenAddr = "localhost:4000" func main() { http.HandleFunc("/", rootHandler) http.Handle("/socket", websocket.Handler(socketHandler)) err := http.ListenAndServe(listenAddr, nil) if err != nil { ...
Go