directory structure and installation skills for GO projects

Source: Internet
Author: User

How the project directory structure is organized, the general language is not specified. But the go language has made the rules so that consistency can be maintained

1, general, a go project under Gopath, there will be the following three directories:

Where the bin holds the compiled executable file, the PKG holds the compiled package file, and SRC holds the project source file. Generally, the bin and pkg directories can be created without the GO command being created automatically (like go install), just create the SRC directory.
For the Pkg directory, someone once asked: I put the package Go under the pkg, how can not ah? He directly put the Go package source files into the pkg. This is obviously wrong. The files in the pkg are generated by Go compilation, rather than manually put in. (General file suffix. a)
For the SRC directory, the source files are stored in the Go, and the source files are organized in the form of packages. In general, creating a new package creates a new folder in the SRC directory.

2. Examples and explanations

For example: I create a new project, test, the starting directory structure is as follows:


In order to compile conveniently, I added a file in it, directory structure:

test/| '--src

The contents of the install are as follows: (Linux)

#!/usr/bin/env Bashif [!-f]; Then    Echo ' install must is run within its container folder ' 1>&2    exit 1ficurdir= ' pwd ' oldgopath= "$GOPATH" Export gopath= "$CURDIR" gofmt-w srcgo install testexport gopath= "$OLDGOPATH" Echo ' finished '

Add this install, do not configure Gopath (avoid adding a go project will add a path to the Gopath)

Next, add a package: Config and a main program. The directory structure is as follows:

test| '--src    |--config    |   '--config.go    '--Test        '--main.go

Note that the package name in Config.go must be the best match for the directory config, and the file name can be random. Main.go represents the main package, and the file name is suggested as Main.go. ( note : when inconsistent, the generated. A file name and directory name are the same, so that when you import, it should be the directory name, and the package name is required when referencing the package.) For example, if the directory is myconfig and the package name is config, then the production static package file is: MYCONFIG.A, referencing the package: import "Myconfig", using the package Member: CONFIG. Loadconfig ())

The code for CONFIG.GO and Main.go is as follows:

Config.go Code

Package Configfunc Loadconfig () {}

Main.go Code

Package Mainimport ("config" "FMT") func main () {config. Loadconfig () fmt. Println ("Hello, go!")}

Next, execute the./install in the project root directory.

The directory structure at this time is:

test|--bin|   '--test||--pkg|   '--linux_amd64|       '--config.a '--src    |--config    |   '--config.go    '--Test        '--main.go# (linux_amd64 means I use the operating system and architecture, you may not be the same)

Where CONFIG.A is generated after the package config is compiled; bin/test is a binary file that is generated

This time can be carried out: Bin/test. Will output: Hello, go!

3. Supplementary instructions

1) package can be multi-level directory, such as: Net/http package, indicating that the source file under the Src/net/http directory, but the source file is the name of the last directory, such as HTTP
When you import a package, you must complete the path, such as: Import "Net/http"

2) Sometimes you will see local import (not recommended), similar syntax:

Import "./config"

When you have such a statement in your code, you will often see errors like this: local import "./config" in the Non-local package

The way I know how to import this is: When you write a simple test script that you want to use with the Go Run command, you can use this type of import.
For example above, move test/main.go to src directory, test directory Delete, modify main.go import "config" to import "./config", then can be executed in src directory: Go run main.go

Visible, local import does not depend on Gopath

4. Install.bat under Windows
@echo Offsetlocalif exist install.bat goto Okecho Install.bat must be run from its Foldergoto end:okset oldgopath=%gopath% Set gopath=%~dp0gofmt-w srcgo Install Test:endecho finished


Directory structure of the HTTP://BLOG.STUDYGOLANG.COM/2012/12/GO project

directory structure and installation skills for GO projects

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: 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.