About the Golang package custom packages structure

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

What I want to say is that the guy Golang's project is a little cumbersome, and the user experience is not as easy as Python or Java. Information on Google's search for Golang package customizations found that the results of the search were not very satisfactory. Not very shallow, that is, very deep, are stretched to the compiler level, the circle ...


The article wrote a little messy, welcome to spray! In addition, the article continues to update, please go to the original address to view the update. http://xiaorui.cc/2016/03/13/%E5%85%B3%E4%BA%8Egolang-package%E8%87%AA%E5%AE%9A%E4%B9%89%E5%8C%85%E7%BB%93%E6%9E%84/

Talk less, just start talking about the use of the Golang package keyword and the project organization code.

There are typically three subdirectories under the Golang project directory:

–SRC Store source code Pkg compiled after the generated file bin compiled executable file

The package name in the. go file in a directory is the same, or you will get a variety of problems when you import. 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 your directory name parking package is inconsistent, then import the upper level of the directory on it.

Golang to build a project there are so many points to note.
1. First of all pay attention to the GOPATH environment, be sure to ensure that your project within the Gopath.
2. Try to keep the directory name consistent with the package so that import does not cause problems when importing custom packages
3. Main is a special package name, similar to the Java main function, go executable program must be under the main packages

We start by creating a new project and customizing a few packages, applying them in main, or allowing different files of the same package to call function methods with each other.

Python #http://xiaorui.cc# Make sure your project is in gopath environment variable, gopath can be used; Define multiple environment locations. $ echo $GOPATH/users/ruifengyun/gg::/users/ruifengyun/project#src is typically used to store specific code. # Ruifengyun at Xiaorui in ~/project/ src [11:42:54]$ pwd/users/ruifengyun/project/src# below is the code structure of the project. # Ruifengyun at Xiaorui in ~/project/src [11:42:26]$ tree|____project| |____bin| |____pkg| | |____darwin_amd64| | | |____h andler.a| | | |____kill.a| | | |____pac.a| | | |____run.a| |____src| | |____handler| | | |____an.go| | | |____h.go| | |___ _main.go| | |____pac| | | |____pac.go| | |____run| | | |____main.go The following is the detailed code specified. # Ruifengyun at Xiaorui in ~/project/src [11:47:32]$ cat Handler/an.gopackage Handlerimport ("FMT") func A () {Hot ( ) FMT. Println ("Func an in ', File is An.go")}# Ruifengyun at Xiaorui in ~/project/src [11:47:37]$ cat Handler/h.gopackage Handl Erimport ("Pac") Func Hot () {PAC. Pacprint ("Hot from PAC func, file was H.go")}# Ruifengyun at Xiaorui in ~/project/src [11:47:41]$ Cat Pac/pac.gopackage PA Cimport ("FMT") funcPacprint (x String) {fmt. PRINTLN (x + "Func pacprint in Pac file")}
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 6667686970717273 #http://xiaorui.cc#一定确保你的项目在GOPATH环境变量里面, Gopath can be used; define multiple environment locations. $ Echo $Gopath/Users/Ruifengyun/GG::/Users/Ruifengyun/Project#src一般是用来存放具体代码的.# Ruifengyun at Xiaorui in ~/project/src [11:42:54]$ pwd/Users/Ruifengyun/Project/src #下面是项目的代码结构.# Ruifengyun at Xiaorui in ~/project/src [11:42:26]$ Tree|____project| |____bin| |____pkg| | |____darwin_amd64| | | |____handler.a| | | |____kill.a| | | |____pac.a| | | |____run.a| |____src| | |____handler| | | |____an.Go| | | |____h.Go| | |____main.Go| | |____pac| | | |____pac.Go| | |____run| | | |____main.GoThe following is the detailed code specified.# Ruifengyun at Xiaorui in ~/project/src [11:47:32]$ CatHandler/ an.Go PackageHandlerImport (    "FMT")func an() {     Hot()    FMT.Println("Func-A, file is An.go")} # Ruifengyun at Xiaorui in ~/project/src [11:47:37]$ CatHandler/h.Go PackageHandlerImport (    "PAC")func Hot() {    PAC.Pacprint("Hot from Pac-func, file is H.go")}# Ruifengyun at Xiaorui in ~/project/src [11:47:41]$ CatPAC/PAC.Go PackagePACImport (    "FMT")funcPacprint(x string) {    FMT.Println(x + "func pacprint in Pac file")}

Before running main, we need to put the above package into the go build, go install

Python <textarea wrap="soft" class="crayon-plain print-no" data-settings="" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;">#http://xiaorui.ccgo build pacgo Install PACGO build Handlergo Install handler</textarea>
1234567 #http://xiaorui.cc Go Build PAC Go Install PAC Go Build Handler Go Install Handler


We build the source code will find in the PKG more *.a files.

Python <textarea wrap="soft" class="crayon-plain print-no" data-settings="" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;"># Ruifengyun at Xiaorui in ~/project/src$ go run Main.gohttp://xiaorui.ccfunc pacprint on Pac Filehot from PAC Func, File is H.gofunc pacprint-Pac filehot from Pac-func, file is H.gofunc pacprint in Pac Filefunc A in, file is An.go </textarea>
1234567 # Ruifengyun at Xiaorui in ~/project/src$ GoRunMain.Gohttp://Xiaorui.CcfuncPacprintinch PACfile Hot from PACfunc , file is h.GofuncPacprintinch PACfile Hot from PACfunc , file is h.GofuncPacprintinch PACfilefunc aninch an, file is an.Go

About Golang Custom package introduction to say so much, later encountered new problems let us elaborate.

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.