Analysis of GO application construction

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed. To understand and flexibly use a language, I feel it necessary to understand the internal mechanisms in which it builds applications. After this period of contact with go, the individual to go to build a program is very satisfied: simple, direct, flexible. Here's a simple analysis of how to build your application in the Go language.
To build a go project, we just need to set up the project working directory and create a new folder named Src in that directory, and then put your source file under SRC. For the sake of understanding, here is an example of a simple project to specify.
Environment variables: Gopath =path/to/gopro: $GOPATH
gopro/ bin/ Hello pkg/ linux_amd64/ example/ mymath.a src/ example/ mymath/ sqrt.go//package MyMath hello/ test.go//package Main, which used the MyMath package
Operation Command: Go Run: To run the application under the current folder, specify the path Go Build: Build the application under the current folder, if you do not specify a path the default build is the go file under the current file Go Install: The corresponding package is generated under PKG, if it is the main package, the corresponding executable file is generated under the bin, and if no directory is specified, the package under the current directory will be install by default.
e.g. In src/example/hello/Run in the directory Go run test.goRun directly Go build test.goGenerate the executable file test to run in any directory: Go install Example/mymathThe package that generates the corresponding CPU architecture in the pkg, e.g. Pkg/linux_amd64/example/mymath.a Go install Example/helloGenerate executable file in bin, e.g. Bin/hello
GO to the official version of the compilation, link these operations are simplified, directly with go+ cmdform of instruction. However, understanding the compile and link process of go can better understand how go is building applications. Go to each CPU architecture has different compiler, linker (this refers to go comes with the compiler, of course, can also be compiled with GCCGO link, this is not detailed), e.g. x86_64 the compile command for the schema is 6g, and the link is 6l. Now we look back. Go InstallThis command, by running Go install-n example/mymath, we see that the inside is actually called the 6g, and then use Packcommand to package the compiled target file into the mymath.a; run it again. Go install-n example/hello, we see the 6g, pack, and 6l commands are called sequentially, which is 6g compiled _go_.6 file, then pack into hello.a package, and finally link to Hello. In addition, the go install/build/run command has a dependency update check mechanism similar to Makefile that will not be recompiled if the source file is not changed.
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.