Created
June 19, 2017 17:58
-
-
Save danmrichards/c17b07339aa71142e3e526264c225c66 to your computer and use it in GitHub Desktop.
Basic golang channel with http
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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