guides on davidchua https://dchua.com/tags/guides/ Recent content in guides on davidchua Hugo -- gohugo.io en-us Wed, 28 Jun 2017 00:00:00 +0000 Sending your Structs across the wire (tcp connection) https://dchua.com/posts/2017-06-23-sending-your-structs-across-the-wire/ Wed, 28 Jun 2017 00:00:00 +0000 https://dchua.com/posts/2017-06-23-sending-your-structs-across-the-wire/ Sending your structs across the wire and receiving them on the other side. Using encoding/gob can help ensure your data structures can receive it on the other side. // server.go package main import ... // Create your custom data struct type Message struct { ID string Data string } func main() { // for purpose of verbosity, I will be removing error handling from this // sample code server, err := net. The Signal Pattern https://dchua.com/posts/2017-06-28-the-signal-pattern/ Wed, 28 Jun 2017 00:00:00 +0000 https://dchua.com/posts/2017-06-28-the-signal-pattern/ The signal pattern can be used to ensure your long-lived processes (ie. servers) are able to handle signals being sent to the process. import ... type Handler struct { } func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { w.Write([]byte(`{}`)) } func main() { h := &Handler{} sigCh := make(chan os.Signal, 1) errCh := make(chan error, 1) signal.Notify(sigCh, syscall.SIGTERM, syscall.SIGINT, syscall.SIGHUP) go func() { err := http.ListenAndServe(":3000", h) errCh <- err }() for { select { case sig := <-sigCh: switch sig { case syscall.