For Go developers, the ultimate compiled single executable is something we’re passionate about while enjoying the convenience of the language. However, things get a little more complicated when we need to distribute more than just executables, for example, a default configuration file; Or a Web service that comes with some simple JS/CSS files or something.

Of course, this is not a problem for experienced old drivers. For example, THE RPM of RH series is the choice of many old drivers. Novices like me also think that the old drivers drive their cars well and can easily manage a distribution package. However, we said that if there is only a little bit of file, I will hit an RPM package, it seems a little pants fart feeling; At the same time, sometimes we run on OSX/Win, which lacks some versatility.

For a number of reasons, some students have wondered why not pack these simple static files into an executable file, so THAT I can distribute only one file. In fact, I quite like this method, the advantage is very simple, but the disadvantage is obvious, naturally distribute the executable file is large. Anyway, take a look with me at how this works.

Go – bindata is introduced

You can see a lot of statically packaged libraries in Go Awesome, but you can’t see Go-bindata, which is weird. And go-binData is significantly more popular and popular than the libraries listed in Awesome.

Go-bindata is simple and the design concept is not hard to understand. Its job is to encapsulate static files in a Go Source Code, and then provide a unified interface through which you pass in the file path, and it will return you the file data for that path. That means it doesn’t care if your file is a character file or a byte file, you handle it and it wraps it.

Nonsense. Here’s an example of go-bindata in action:

As you can see here, Line 1 is an example of using go-bindata. It says that /etc/config. json is wrapped with Go-bindata, and then I’m going to use it directly so that I don’t have to worry about the static file location when I run it. I’ll just look at /etc/config. json.

Go – bindata use

First, I’ll introduce my directory structure, because it’s important to use Go-bindata later:

In the root directory I defined the interface file, and then in the etc directory I placed my configuration file: config.json, which is also the configuration file I want to package, and then in file/service.go I implemented the code that needed to read the file.

1. Use Go-bindata to package static files

The first step is to use go-bindata to read the configuration file and then regenerate it into a GO file, using the following command:

$ go-bindata  -pkg etc -o etc/bindata.go etc/
Copy the code

You can use go-bindata –help to view the parameters of go-bindata. After using the above command, you will find a bindata.go file in the etc directory. Next we will use this file instead of the /etc/config. json file.

Using static files

The code for using static files is also very simple, we just need to remember the relative path of the go file we just generated, and then use:

Asset("etc/config.json")
Copy the code

This statement reads the static file. Example code for the entire service is:

Line 5 here is the code that reads the static file using the bindata.go we just generated.

conclusion

This article is a very brief introduction to how to use Go-binData to package static files and how to use them in your project. Go-bindata –help is a simple way to use go-bindata –help, so it is suitable for some uncomplicated scenarios. However, this means that its functionality may also be limited; for example, http.filesystem is not supported by default, so you have to find other plug-ins to support it.

Reference

  1. Go-Bindata

Traveler soy Sauce
liuliqiang.info