Go Language Package static file

Source: Internet
Author: User

For Go language developers, while enjoying the language convenience, the final compiled single executable file is also our passion. However, when it comes to what we need to distribute and not just the executable, things get a little bit more complicated, such as the need to distribute a default configuration file, or a Web service that comes with some simple js/css files.

Of course, this is not a problem for experienced old drivers, such as the RH series RPM is the choice of many old drivers, such as my novice also feel that the old drivers of this car is good, it is convenient to manage a distribution package. However, for us to say if only a little bit of paper, I would like to play a rpm bag, it seems a little off the feeling of farting, at the same time, sometimes we may be in the Osx/win platform running, which lacks a certain versatility.

Based on a lot of considerations, some students think why do not put these simple static files into the executable file, so I will only distribute a file on it, in fact, I also like this way, the advantage is very simple, the disadvantage of course is also obvious, natural distribution executable file is big. Anyway, gains, may I see how this approach is implemented.

Go-bindata Introduction

In the Go language Awesome you can see a lot of static packaging library, but you do not see go-bindata, haha, this is very strange ha. And the Go-bindata is significantly more popular and popular than the libraries listed in Awesome.

Go-bindata is simple, and the design concept is not difficult to understand. Its task is to say that the static file is encapsulated in a Go language Source Code, and then provide a unified interface, you pass through this interface to the file path, it will give you to return the corresponding path of the file data. This means that it doesn't care whether your file is a character file or a byte-type, you handle it yourself, it just wraps it.

Nonsense, here is an example to show the actual use of Go-bindata:

?

As you can see here, line1 is an example of using go-bindata, which is said to etc/config.json wrap it through go-bindata, and then use it directly below so that I don't have to focus on the exact location of the static file when running, I only look for etc/config.jsonjust fine.

Go-bindata use

First of all, let me introduce my directory structure, as this will have a lot to do with the use of Go-bindata in the upcoming introduction:

?

In the root directory I am the interface file defined, and then the etc directory is placed my configuration file: config.json , this is also I want to package the configuration file, and then file/service.go inside I am the implementation of the need to read the file code.

1. Packaging static files with Go-bindata

As I said earlier, Go-bindata is to package a static file into a go file, so the first step is to use Go-bindata to read the configuration file, and then build the go file, using the command as follows:

$ go-bindata  -pkg etc -o etc/bindata.go etc/

For Go-bindata parameters, you can use go-bindata --help view. After using this command, you will find that there is etc one more file in the directory bindata.go , and we will use it instead of the file in the next step etc/config.json .

Working with static files

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

Asset("etc/config.json")

Such a statement can be read to a static file, the entire service sample code is:

?

Line 5 Here is a piece of code that we've just generated bindata.go to read a static file.

Summarize

This article is very simple to describe how to use Go-bindata to package static files and how to use them in your project. From the introduction of this article should be able to see go-bindata or relatively simple, not too complex way, the use go-bindata --help will also find that the parameters are relatively small, so suitable for some requirements are not complex scenarios. However, this means that its functionality may also be limited, for example, it is not supported by default http.FileSystem and can only be found with other plug-in support.

Reference
    1. Go-bindata

Go Language Package static file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.