In my first iteration, I got the following to compile and work:
package main
import (
"fmt"
"sync"
)
var wg sync.WaitGroup
func routineHandle (query string, ch chan <- string) {
ch <- query
wg.Wait()
}
func ping () {
ch := make(chan string)
wg.Add(1)
go routineHandle("testquery",ch)
wg.Done()
msg := <-ch
fmt.Println("Channel Message",msg)
}
func main () {
ping()
}
This successfully uses 1 channel to perform the goroutine routineHandle
Now, I want to add web server capabilities which perform the following:
- Listens on a port and accepts/returns requests
- Hooks into the routineHandle so we can utilize that goroutine as an Api Server Method
My code is on a linux box without a gui so I don't know how to test the web server capabilities.
My code looks like as follows:
package main
import (
"fmt"
"sync"
"net/http"
)
var wg sync.WaitGroup
func routineHandle (query string, ch chan <- string) {
ch <- query
wg.Wait()
}
func ping (w http.ResponseWriter, r *http.Request) {
ch := make(chan string)
wg.Add(1)
go routineHandle("testquery",ch)
wg.Done()
msg := <-ch
//fmt.Println("Channel Message",msg)
w.Write([]byte msg)
}
func main() {
http.HandleFunc("/",ping)
http.ListenAndServe(":1234",nil)
}
You'll notice a few additions with my second piece of code:
- I added the
net/http
package - I added the
http listener
to themain method
- I added response writer and request parameters to the ping function
- I changed from
fmt.Println()
toc.Write
The end goal would be for typing in a query, and then using that query in the routineHandle
goroutine
Like I Said Though, I don't know how to test this final implementation on an ubuntu box without a gui
One last thing to note. If you notice any issues PLEASE let me know. I wonder if running a goroutine inside a http server would cause an issue