Golang Project Self-management

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

Friends who have worked with VS or Eclipse must be on the left column (or may be on the right or bottom or hidden) Engineering Directory Memory Party was fresh. Especially when there are a lot of dependencies on packages and libraries, the directory structure is more complex. In fact, this is not a big problem. The nature of being familiar with your project adapts to this structure. But when you go to see some c/cpp projects, their directory structure is different. Although there are some "src/bin/lib" directories that everyone understands. But each project is different from each other. This can be frustrating when touching someone else's project. And when you have multiple projects. If you do not arrange their own good location, it will bring a lot of trouble for themselves. Remember when Xiao Ming asked you for the code, when you were flying between the various directories to find the scene? And all this in the Golang has been a strong guarantee. Golang at the language level (or more rationally at the compiler tool level) prescribes a developer's directory structure and the geographic location of various files, and this management is inherently responsive to the idea of open source and caters to git, Mercury's management model. Imagine. You see in a fixed directory of your own Src/github.com/nsf/gocode will think. Is that what you want? You don't have to think about a project on GitHub and see what he does in the readme. In addition, there is no need to makefile, Autotools management bin files and the location of the Lib file. (many here are relative to the same need to compile the c/cpp, do not like to please around).

Project Self-Management steps

This is not just a recommendation, but part of it is mandatory. For example, you can use a directory structure like github.com/nsf/. However, there must be bin and src directories.

Installing Golang

First of all, of course, to install our Golang. RPM tied yum install golang up and ended the fight, and Deb's, of course, apt-get install golang took care of it. If you do not have a network, you can manually go to http://golang.org download the tar package back and then unzip. Here you need to set the bin directory of Go to path.

Export PATH = $ PATH : Your_go_dir / bin /

Run go version to see if you can see the release information of Go normally. If you see it, the installation is successful.

The students who have installed JRE/JDK know that they are finished installing Java-related environment variables. So what environment variables do you want to set after installing Golang? How many settings? See the above version of the information, in fact, I would like to say not set is also can see java -version the success of information. But.. But in order to better meet our above project self-management requirements, we still have to set the environment variables, in order not to Golang, only need to set an environment variable. He is gopath. Use this variable to find the home directory of the Golang project, or the project directory.

Gopath = Your_go_project_dir

This is YOUR_GO_PROJECT_DIR not the above YOUR_GO_DIR , if you install through the software source is not the above is not YOUR_GO_DIR . Here's where YOUR_GO_PROJECT_DIR you put your code in the future with Golang writing programs. For example, I set the ~/projects/golang . After the end in order to let our Golang write program can be easily run after compiling, instead of CD to a directory and then./xxx to run, we add $gopath/bin to the PAHT environment variable. This, of course, may also create a safe path. But we are using Golang to do development, the development of the machine naturally does not matter, let alone similar tools such as gocode have such a setting will be more convenient to operate it. So we set him up, too.

Export PATH = $ PATH : $ Gopath / bin

Good. Our basic work here is finished. That is, the installation of Golang. Wait, since we have the $gopath, then we must build the relevant catalogue. Build bin and src directories in turn

mkdir - P $ Gopath / src $ Gopath / bin

All right. Perfect.

Compile operation

With the preparation above, create a basic directory and set the environment variables to start Hello World (programming don't say don't know Hello World oh). First write the source of Hello World.

mkdir -P $Gopath/src/GitHub.com/ You/HelloWorldCD $Gopath/src/GitHub.com/ You/HelloWorldEmacs HelloWorld.Go Package MainImport ("FMT")func Main(){    FMT.Print("Hello World\ n")}

With the source code after how to compile it? can use CGO,GCC to Golang support, that is too self-help, not what we say in the "self-management" to say, here to talk about a more convenient way. Help us automate the management process with the Go Tools series tools.

First we want to see the results of this program immediately, then we can

Go Run HelloWorld . Go

For quick verification.

After the verification is done. We will eventually have to compile him into a binary file. This time with

Go Build

Run in the directory where the Helloworld.go is located. We will see that the executable file is generated in the current directoryhelloworld

. / HelloWorld

You can see the results of the program's output.

When the program is published. Run

Go Install

The corresponding file will be generated in the $GOPATH/bin/directory. Here we can take go build the resulting file as a debug version for the mode, when the mode OK, turn off debug log, and then go install get the release version.

The self-management is mentioned here and there, because it is in such a directory format that this set of tools is effective, and the source code structure is reasonable. Although the structure under the SRC directory is not mandatory, it is good to look at the "Webserver.com/user/project" way and when you want to put it on GitHub or bitbucket. As long as the CD is in the appropriate directory:

git Remote Add git Add * git Commit git Push

Management can be completed.

It says the creation of the executable file. So how to build a package?

The first thing to do is to replace the main function name with the C/cpp, Java, the entry cannot be compiled into the package. And then run

Go Build / Intall

The corresponding package is generated in the current directory or in the $GOPATH/pkg/bin_platform/github.com/you/directory, and the package is the file name. A, go tools generates a static library by default.

It can then be passed in other code.

Import ("Github.com/you/yourpackage")

The way the package was referenced.

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.