Coder Social home page Coder Social logo

asset's Introduction

#asset-go

asset helps golang developers to reference external assets inside binaries.

##The goal

  • Configurable base path for assets
  • Relative paths done right

###What wait! where is the trouble? Golang does not have a built-in bundle manager like many popular languages do. ie. It does not generate a portable .app .jar container that includes everything the app needs (binaries and assets).

###Possible solution: Absolute paths

file, err := os.Open("/var/www/image.png")

Disadvanges:

  • Your assets and binaries are spread around the system.
  • Tricky for version control
  • No cross platform

###Possible solution: Relative paths

file, err := os.Open("image.png")

Disadvanages:

  • The binary MUST by executed in the same working path.
$ /var/bin/myserver

does not work. You have to change the working path in order to get it run properly:

$ cd /var/bin/
$ ./myserver

##asset.go solution

file, err := os.Open(asset.Path("image.png"))

asset knows where the executable is located, so there is not need to change the working path.

##Understanding asset

If the binary is located at /go/bin/myserver .

func main() {
   fmt.Println(asset.Path("image.png"))
}
$ cd /root
$ /go/bin/myserver
/go/bin/myserver/image.png

$ myserver
/go/bin/myserver/image.png

$ cd test
$ ./symlink_myserver
/go/bin/myserver/image.png

`asset.Path() returns the same path, no matter how the binary is executed.

###Configuring asset asset.Configure(DEFAULT_VALUE, INPUT_FLAGS)

Let's focus in DEFAULT_VALUE by the moment.

func init() {
   asset.Configure("resources", asset.NoInput)
   fmt.Println(asset.Path("image.png"))
}
/go/bin/server/resources/image.png

How about an absolute path?

asset.Configure("/var/www/resources", asset.NoInput)
fmt.Println(asset.Path("image.png"))
/var/www/resources/image.png

####INPUT_FLAGS

func init() {
   asset.EnvVariableName = "SERVER_ASSETS"
   asset.Configure("", asset.Environment)
   fmt.Println(asset.Path("image.png"))
}

Now you can change the base path with an environment variables:

$ export SERVER_ASSETS="/var/myserver/resources"
$ myserver
/var/myserver/resources/image.png

A flag can be also used:

asset.Configure("", asset.Environment | asset.Flag)
myserver -apath="/myassets"

asset's People

Contributors

manucorporat avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

liujiawm

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.