code
stringlengths
10
1.34M
language
stringclasses
1 value
// +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
// +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" "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) // HL } } var partner = make(chan io...
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" "net/http" "time" "code.google.com/p/go.net/websocket" ) const listenAddr = "localhost:4000" func main() { go netListen() // HL http.HandleFunc("/", rootHandler) http.Handle("/socket", websocket.Handler(socketHandler)) err := http.ListenAndServ...
Go
package main import ( "fmt" "os" ) const moved = ` The present tool has moved to the go.tools repository. Please install it from its new location: go get code.google.com/p/go.tools/cmd/present ` func main() { fmt.Print(moved) os.Exit(1) }
Go
// Copyright 2009 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. package websocket import ( "bufio" "crypto/tls" "io" "net" "net/http" "net/url" ) // DialError is an error that occurs while dialling a websocket server...
Go
// Copyright 2009 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. package websocket // This file implements a protocol of Hixie draft version 75 and 76 // (draft 76 equals to hybi 00) import ( "bufio" "bytes" "crypto/md5"...
Go
// Copyright 2011 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. package websocket // This file implements a protocol of hybi draft. // http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-17 import ( "bufio" "...
Go
// Copyright 2009 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. package websocket import ( "bufio" "fmt" "io" "net/http" ) func newServerConn(rwc io.ReadWriteCloser, buf *bufio.ReadWriter, req *http.Request, config *Co...
Go
// Copyright 2009 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. // Package websocket implements a client and server for the WebSocket protocol // as specified in RFC 6455. package websocket import ( "bufio" "crypto/tls" ...
Go
// Copyright 2010 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. package html // All entities that do not end with ';' are 6 or fewer bytes long. const longestEntityWithoutSemicolon = 6 // entity is a map from HTML entity n...
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. // Package atom provides integer codes (also known as atoms) for a fixed set of // frequently occurring HTML strings: tag names and attribute keys such as "p" /...
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 ignore package main // This program generates table.go and table_test.go. // Invoke as // // go run gen.go |gofmt >table.go // go run gen.go -test |...
Go
// Copyright 2010 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. package html import ( "bytes" "strings" "unicode/utf8" ) // These replacements permit compatibility with old numeric entities that // assumed Windows-1252 ...
Go
// Copyright 2010 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. package html import ( "bytes" "io" "strconv" "strings" "code.google.com/p/go.net/html/atom" ) // A TokenType is the type of a Token. type TokenType uint...
Go
// Copyright 2011 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. package html import ( "bufio" "errors" "fmt" "io" "strings" ) type writer interface { io.Writer WriteByte(c byte) error // in Go 1.1, use io.ByteWriter...
Go
// Copyright 2010 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. package html import ( "errors" "fmt" "io" "strings" a "code.google.com/p/go.net/html/atom" ) // A parser implements the HTML5 parsing algorithm: // http...
Go
// Copyright 2011 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. package html // Section 12.2.3.2 of the HTML5 specification says "The following elements // have varying levels of special parsing rules". // http://www.whatwg...
Go
// Copyright 2011 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. package html import ( "strings" ) func adjustAttributeNames(aa []Attribute, nameMap map[string]string) { for i := range aa { if newName, ok := nameMap[aa[...
Go
// Copyright 2011 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. package html import ( "code.google.com/p/go.net/html/atom" ) // A NodeType is the type of a Node. type NodeType uint32 const ( ErrorNode NodeType = iota T...
Go