Handling LD_FLAGS in multiple versions of Golang

Oct 1, 2015 Golang

Introduction

Recently, many shells and Jenkins Jobs were painted with deprecation warnings for users of Golang 1.5. Deprecation warnings are nothing to ignore, since it’s a slap on the back of your head to maintain your code. This specific warning has to do with how LD_FLAGS are read by Go during the build process.

Now for you gogetters out there running 1.5, do you know what happens if you update your code to 1.5 standard and try to run it on a 1.4 system? Guess what, it’s broken. For those who want continue to run 1.5 locally (or remote), and need to still support 1.4, here’s a simple Makefile approach: if/else.

Building Logic

We use both go and godep binaries to build our packages. As a result, we need to know the go version for godep. Luckily, that’s still provided when running a godep version. As a result, we need to use a findstring match, rather than a string equals match.

Here is a complete example:

Conclusion

In the above example, we allow Makefile to determine the location of the binary, rather then setting it in stone, sometimes PATH’s and packages aren’t consistent across environments. We take the discovered Go/Godep binary, grab it’s version, and run a ifneq to evaluate the existence of the version within the string. That way, we can use the updated syntax on newer systems to silence those pesky warnings while still providing support older platforms.


comments powered by Disqus