Golang Project directory Structure organization

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

In fact, Golang Engineering management is quite simple, completely use the directory structure and the package name to derive the engineering structure and construction sequence.

Of course, the first thing to say is the environment variable $gopath, the construction of the project depends on it. Let's just say, to build a project, add this project to $gopath, multiple projects with ";" Separated.

There are typically three subdirectories under the Golang project directory:

    • SRC Store Source code
    • Files generated by PKG after compilation
    • Executable file generated after bin compilation

What we focus on is actually the directory structure in the SRC folder.

For example, more than anything, the directory structure is as follows:

<proj>   |--<src>       |--<a>           |--<a1>               |--al.go           |--<a2>               |--a2.go       |--<b>           |--b1.go           |--b2.go       |--<c>           |--c.go   |--<pkg>   |--<bin>

Each file code:

A1.go

" FMT "   func PrintA1 () {    fmt. Println ("a/a1")}

A2.go

Package A2 " FMT " func PrintA2 () {   fmt. Println ("a/a2")}

B1.go

" FMT "   func printB1 () {    fmt. Println ("b.b1")}

B2.go

Package B   " FMT "   func printb () {    printB1 ()    FMT. Println ("b.b2")}

Note the packages in the B1.go b2.go are the same, i.e. only one package is allowed under the same directory

C.go

Package main  import (    "a/a1""    a/a2 "    "b"  ) Func main () {    A1. PrintA1 ()    A2. PrintA2 ()    b.printb ()}

Add Proj to $gopath:

In Windows there is a problem to note, is the Chinese path problem, if the Proj path has Chinese, then in the compilation may be problematic, I was tortured by this problem for more than half an hour, left and right to pick not wrong, is not compile.

Go build/a1go Install a/a1go Build a/a2go Install a/a2go build bgo Install BGO Bui LD CGO Install C

Executes the relevant command.

Can see the Pkg folder under the generated, a folder, B.a,a folder under the a1.a, a2.a

The Bin folder has C.exe (of course, there is also c.exe in the location where go build C is executed)

Run under C.exe

It can be seen that the categories are very clear.

In fact, the two. Go files under the B folder can be combined into one without any effect.

The name in the package should be the same as the directory name, so that you can import the directory name directly when you import it. If the two are inconsistent, such as the above B1.go, B2.go inside the package is the package bbbbb, then import in the C.go to import "B", and then the following B. PRINTB (), you need to change to BBBBB. PRINTB ()

Directory Structure of Go Project I feel quite clear about it.

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.