Golang project directory structure Organization

Source: Internet
Author: User

In fact, golang's engineering management is quite simple. It uses the directory structure and package name to derive the engineering structure and build sequence.

Of course, the first thing we need to talk about is the environment variable $ gopath, which is fully dependent on the project construction. To build a project, add it to $ gopath. Separate multiple projects.

The golang project directory generally has three subdirectories:

  • SRC stores source code
  • PKG files generated after compilation
  • Executable files generated after Bin Compilation

We should focus on the directory structure in the SRC folder.

For example, 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>

Code of each file:

A1.go

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

A2.go

package a2 import "fmt" func PrintA2(){   fmt.Println("a/a2")}

B1.go

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

B2.go

 package b  import "fmt"  func PrintB(){    printB1()    fmt.Println("b.b2") }

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 with the Chinese path. If the proj path has a Chinese character, a problem may occur during compilation. I have been suffering this problem for more than half an hour, there is no error in picking left and right, that is, compilation is not allowed.

 

go build a/a1go install a/a1go build a/a2go install a/a2go build bgo install bgo build cgo install c

Execute related commands.

You can see that the PKG folder is generated, and the folder, B. a, and a folders contain a1.a and a2.a.

The binfile contains c.exe (of course, c.exe is also available in the location where the go build C is executed)

Run c.exe

It can be seen that the categories are very clear.

In fact, the two. Go files in the B folder can be merged into one, without any impact.

The package name should be the same as the directory name, so that you can directly import the directory name during import. If the two are inconsistent, for example, the package in b1.go and b2.go is package bbbbb. import "B" when importing in go, and then the following B. printb (), you need to change it to bbbbb. printb ()

I feel quite clear about the directory structure of the Go project.

Golang project directory structure Organization

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.