Skip to content

Instantly share code, notes, and snippets.

@danmrichards
Created June 19, 2017 17:58
Show Gist options
  • Save danmrichards/c17b07339aa71142e3e526264c225c66 to your computer and use it in GitHub Desktop.
Save danmrichards/c17b07339aa71142e3e526264c225c66 to your computer and use it in GitHub Desktop.
Basic golang channel with http
package main
import (
"fmt"
"io"
"net/http"
)
func main() {
passer := &DataPasser{logs: make(chan string)}
go passer.log()
http.HandleFunc("/2", passer.handleHello)
http.ListenAndServe(":9999", nil)
}
type DataPasser struct {
logs chan string
}
func (p *DataPasser) handleHello(w http.ResponseWriter, r *http.Request) {
p.logs <- r.URL.String()
io.WriteString(w, "Hello world")
}
func (p *DataPasser) log() {
for item := range p.logs {
fmt.Println("2. Item", item)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment