go
How do you name a multi-term package in golang?
I think there are many many many options. A few of which are: If your project is only focused on supervising rhino horn you could name your project rhino-horn-supervisor and not package anything just have flat top level files main.go, hornoutput.go, or w/e If supervising rhino horn is one component of a larger application there … Read more
How to serve documentation using godoc together with go modules?
The issue isn’t modules so much as GOPATH. There’s a github issue thread that discusses this in more detail: https://github.com/golang/go/issues/26827 That thread has evolved a workaround that uses a docker container to run a godoc server with GOPATH set to the base of your dev tree. That godoc server will serve docs for all of … Read more
generate godoc documentation for an entire project?
is there a canonical way to generate documentation for offline use even using godoc? Go 1.12 (February 2019) is clearer on that: godoc and go doc In Go 1.12, godoc no longer has a command-line interface and is only a web server. Users should use go doc for command-line help output instead. go doc now … Read more
Difference between *string and sql.NullString
SQL has different null values than Golang. If you look at the definition of sql.NullString then this is what you get: type NullString struct { String string Valid bool // Valid is true if String is not NULL } As you can see, sql.NullString is a way to represent null string coming from SQL (which … Read more
How do you use Go 1.16 embed features in subfolders/packages?
I finally figured it out… You can keep the templates folder in the main folder and embed them from there. Then you need to inject the FS variable into the other handler package. It’s always easy after you figure it out. e.g. package main //go:embed templates/* var templateFs embed.FS func main() { handlers.TemplateFs = templateFs … Read more
How do I serve CSS and JS in Go
http.Handle(“https://stackoverflow.com/”, http.FileServer(http.Dir(“css/”))) Would serve your css directory at /. Of course you can serve whichever directory at whatever path you choose. You probably want to make sure that the static path isn’t in the way of other paths and use something like this. http.Handle(“/static/”, http.StripPrefix(“/static/”, http.FileServer(http.Dir(“static”)))) Placing both your js and css in the directory … Read more
Passing an optimization flag to a Go compiler?
Actually no explicit flags, this Go wiki page lists optimizations done by the Go compiler and there was a discussion around this topic in golang-nuts groups. You can turn off optimization and inlining in Go gc compilers for debugging. -gcflags ‘-N -l’ -N : Disable optimizations -l : Disable inlining
Concisely deep copy a slice?
Not sure which solution is fastest without a benchmark, but an alternative is using the built in copy: cpy := make([]T, len(orig)) copy(cpy, orig) From the documentation: func copy(dst, src []Type) int The copy built-in function copies elements from a source slice into a destination slice. (As a special case, it also will copy bytes … Read more
How to parse non standard time format from json
That’s a case when you need to implement custom marshal and unmarshal functions. UnmarshalJSON(b []byte) error { … } MarshalJSON() ([]byte, error) { … } By following the example in the Golang documentation of json package you get something like: // First create a type alias type JsonBirthDate time.Time // Add that to your struct … Read more