Directory structure of the GO Project

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

How the project directory structure is organized, the general language is not specified. But go language this aspect has made the stipulation, this can maintain the consistency, achieves unifies, the rule is more clear.

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

|--bin|--pkg|--src

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, Myfirst, the starting directory structure is as follows:

myfirst--    |--SRC

In order to compile conveniently, add an install file, directory structure:

myfirst        | -install.bat    |-src

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

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

Myfirst    | -Instal.bat    | -src        |-- config                |-- config.go|-                    myfirst|--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 resulting. 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 the package is referenced.) 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 ONFIG.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! " )}

Install.bat Configuration Instructions:

@echo offsetlocal if Goto   from its foldergoto  end:okset oldgopath=%gopath%set gopath=%~-w srcgo install Myfirst:endecho finished

Note: There should be no spaces between colons and OK.

Open the command line, locate the D:\myfirst directory, enter install, and do the following:

When you are done, generate the PKG directory:

Command-line prompt: Config.loadconfig not defined.

After repeated retries, the Install.bat is modified as follows:

@echo offsetlocal if Goto   from its foldergoto  end:okset oldgopath=%gopath%set gopath=%cd%-w srcgo install Myfirstset gopath=Oldgopath:endecho finished 

Config.go modified as follows:

Package Configfunc Loadconfig () {}

The function name starts with uppercase.

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.