The directory structure of the Go Project is detailed _golang

Source: Internet
Author: User
Tags goto

How the project directory structure is organized, the general language is not defined. However, the go language is defined so that consistency can be maintained.

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

Copy Code code as follows:

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

The bin stores the compiled executable file, pkg stores the compiled package file, and SRC holds the project source file. Generally, the bin and pkg directories are not created, and the Go command is created automatically (for example, going install), simply by creating the SRC directory.

For the Pkg directory, someone once asked: "I put the bag in the go under PKG, how can not ah?" He put the source file of the go package directly into the pkg. This is clearly not the right thing to do. The files in the pkg are generated by Go compilation, not manually. (General file suffix. a)

For SRC directories, where source files are stored, the source files in go are organized in the form of packages (package). Typically, creating a new package creates a new folder in the SRC directory.

2, the example explains

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

Copy Code code as follows:

Test--|--src

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

Copy Code code as follows:

test/
|--Install
'--src

The contents of install are as follows: (Linux)

Copy Code code as follows:

#!/usr/bin/env Bash

if [!-f Install]; Then
Echo ' Install must be run within its container folder ' 1>&2
Exit 1
Fi

Curdir= ' pwd '
Oldgopath= "$GOPATH"
Export gopath= "$CURDIR"

Gofmt-w SRC

Go install test

Export gopath= "$OLDGOPATH"

Echo ' finished '

The reason for adding this install is to not configure Gopath (avoid adding a go item to add a path to the Gopath)

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

Copy Code code as follows:

Test
|--Install
'--src
| |-Config
| '--Config.go
'--Test
'--Main.go

Note that the package name in the Config.go must be the same as the directory config, and the filename can be random. Main.go represents the main package, and the filename is recommended as main.go. (Note: When inconsistent, the generated. A file name is consistent with the directory name, so that when you import, it should be the directory name, and the package name is required when referencing the package.) For example: The directory is myconfig, 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

Copy Code code as follows:

Package Config

Func Loadconfig () {

}

Main.go Code

Copy Code code as follows:

Package Main

Import (
"Config"
"FMT"
)

Func Main () {
Config. Loadconfig ()
Fmt. Println ("Hello, go!")
}

Next, execute in the project root directory./install

The directory structure at this time is:

Copy Code code as follows:

Test
|--Bin
| '--Test
|--Install
|--PKG
| '--LINUX_AMD64
| '--CONFIG.A
'--src
| |-Config
| '--Config.go
'--Test
'--Main.go
(linux_amd64 means that I use the operating system and architecture that you might not have)

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

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

3. Supplementary explanation

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

2 sometimes see the local import (not recommended), syntax similar to this:

Copy Code code as follows:

Import "./config"

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

The way I've learned to use this type of import is when you write a simple test script and want to use the Go Run command.
For example, the above example, the Test/main.go moved to the SRC directory, the test directory deletion, modify the import "config" in Main.go as import "./config", and then can be executed in the SRC directory: Go run main.go

Visible, the local import does not depend on the Gopath

4, Windows under the Install.bat

Copy Code code as follows:

@echo off

Setlocal

If exist Install.bat goto OK
Echo Install.bat must being run from its folder
Goto END

: OK

Set oldgopath=%gopath%
Set Gopath=%~dp0

Gofmt-w SRC

Go install test

: End
Echo Finished

Note, there should be no space between the colon and OK, but it will always be turned into an expression by WordPress. Sweat......

5, update the log

1) 2012-12-05 Release
(2) 2013-04-13 revision: The directory name can be different from the package name, but it is recommended to be consistent; Change the make file name to install

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.