Package front-end website to go program

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Coolpy5 released soon, the new version requires the service-side program is just a running file, after going through the go template countless pits, and finally gave up, so or to the front-end independent development of a pure front-end program, However, the GO program directly builds the word does not support the front-end program compiled into the final generated files, so there is a way to package the front-end program into the final generated run file.

The package used is Github.com/geertjohan/go.rice

A Rice.exe (window platform) will be downloaded or compiled, and the Linux platform should download the package and compile it yourself.

Configure go env To configure the GO environment variables, in this case the file engineering path is: E:\Coolpy5Sub\Coolpy5Sub\bin

E:\coolpy5sub\coolpy5sub\bin>go env
Set GOARCH=AMD64
Set gobin=
Set Goexe=.exe
Set GOHOSTARCH=AMD64
Set Gohostos=windows
Set Goos=windows
Set Gopath=e:\coolpy5sub\coolpy5sub\bin
Set gorace=
Set Goroot=c:\go
Set GOTOOLDIR=C:\GO\PKG\TOOL\WINDOWS_AMD64
Set Go15vendorexperiment=1
Set CC=GCC
Set gogccflags=-m64-mthreads-fmessage-length=0
Set cxx=g++
Set cgo_enabled=1

Configure the Go environment parameter with set GOPATH=XXXXX if required to be configured under win platform

File example table:

SRC is the Go src directory, including all of its own custom packages and third-party packages, Main.go is the program entry point, www folder is the entire front-end program.

In Main.go, reference rice package and the WWW folder as a file service host a service:

Code

Import "Github.com/geertjohan/go.rice"

http. Handle ("/", HTTP. Stripprefix ("/", HTTP. Fileserver (rice. Mustfindbox ("www"). Httpbox ())))
http. Listenandserve (": 8000", nil)

The above code needs to be aware that the folder name pointed to by WWW is directly escaped to the relative path, HTTP. Handle ("/", that is, by requesting the http://127.0.0.1:8000 can be accessed, if the external network, please switch to the relative address of the extranet.

Pre-preparation is complete:

1.cmd works at present and executes the rice embed-go directive, note that Rice.exe must also be in the same directory

After execution will produce a rice-box.go file in the current directory, this is a very important file, it is automatically generated a go file, the WWW folder all the files are configured as static resources, so when we execute the build command here, the Rice-box.go is directly compiled into the resulting file.

Next execute the Go build main.go instruction

After execution is the final running program.

Main.exe is the final compiled run file, which is already a standalone file, and the front end program is packaged in.

Open source cross platform high performance IoT platform: http://icoolpy.com

Related Article

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.