Golang cheat sheet and various tips
|Title:||Go cheat sheet and tips|
|Created:||22:00 on Sunday, 15. July 2018|
|Modified:||22:00 on Sunday, 15. July 2018|
A curated list of things I encountered when using the Go language. Most of this stuff is probably mentioned elsewhere, so this should be seen as some kind of personal reference. You may or may not find anything useful here.
GCC-GO linker errors
Sometimes, when using Gcc-go instead of the default Go compiler, an error message like the following may appear during the build command.
[...] $WORK/b001/_pkg1_.a(_x009.o): undefined reference to symbol 'pthread_mutexattr_settype@@GLIBC_2.1' /lib/libpthread.so.0: error adding symbols: DSO missing from command line collect2: error: ld returned 1 exit status
This indicates a missing LDFLAG, in our case, the instruction to link against
missing from the linker command line. It’s easy to fix, just pass the missing
-gccgoflags to the build command. The -pthread option not only instructs the linker to link
against libpthreads, it also defines all macros that are needed to ensure re-entrant code is
produced during compilation.
[alex@warpcore:/home/alex/go/src/testgo]$> go-8 build -gccgoflags=-pthread dbtest.go
go-8 is the wrapper to
gccgo, using GCC8 on Suse Linux distributions. This might be
different for your distribution and depends on the gccgo version. GCC8 based gccgo compiler is
compatible with Go version 1.10.
Getting gocode and go-mode in Emacs to work
This is for Go version 1.10. First, install the mdempsky fork of gocode as the old gocode might be incompatible with Go 1.10 due to changes in the binary package format.
- https://github.com/mdempsky/gocode - do a
go get github.com/mdempsky/gocodeto install.
The resulting binary will be in
~/go/bin on *iX systems), so you can either
add this directory to your
$PATH or copy gocode to
/usr/bin if you have permissions.
In Emacs, it’s just a matter of installing
go-mode. Assuming, you have a working MELPA setup,
simply do a
M-x package-install <RET> go-mode <RET>. For auto-complete, I prefer
which case, the Go backend
company-go is required. Install it like
go-mode before. Go-mode itself
requires minimal configuration - if
gocode is in your $PATH it will be found and used
Note that gocode depends on a properly configured
$GOPATH environment variable and may not work at
all without it, particularly if you do not use the default go directory, which is normally set to
~/go by default. Also, gocode only recognizes packages built and installed with the default go
compiler. Personally, I use gccgo only for release builds and usually install all packages with both
Sometimes (rarely though), gocode will get stuck and subsequently refuse to offer more completions
for reasons unknown. I’ve not yet found a pattern or way to reproduce it. If that happens, simply do a
$PATH/to/gocode close (or simply
gocode close if it is in your $PATH). This will terminate
gocode and emacs will happily re-launch it at the next attempt.
You can write a comment
Please stick to common netiquette when posting comments. Avoid any form of personal attacks, harassment, hate speech, sexism, racism or otherwise inappropriate content. Comments that violate common rules of civilized communication among humans will be deleted without further notice.