"Golang" Study note 2-code directory structure, application compilation, custom package, and remote package reference

Source: Internet
Author: User
Tags using git

Objective

In the previous article we described how to configure the Go development environment under Windows installation. This article describes the directory structure of code Golang, application compilation, custom packages, and references to remote packages.

Code directory structure

At the end of the Gopath we set the SRC directory is the main directory of our next development program, all the source code is placed under this directory, the general practice is a directory of a project .

For example: %gopath%/src/mymath This directory represents the MyMath application package or executable application (depending on whether the packages are main or otherwise, If main is an executable application, non-main is the application package. )

So when we create a new application or a code package is a new folder in the SRC directory, the folder name is generally the name of the code package. Of course, the new multi-level directory is also possible, for example, under the SRC new directory %gopath%/src/github.com/hebinz/optestlink then this package path is "github.com/hebinz/ Optestlink ", which is the directory level from the SRC directory.

Here I take mymath as an example of how to write an application package, execute the following code, switch to the D drive (the disk where we previously set the Gopath), and then switch to the SRC directory under Gopath to create the MyMath folder:

D:cd %GOPATH%/srcmkdir mymath

New file Sqrt.go, add the following:

package mymathfunc Sqrt(x float64) float64 {    z := 0.0    for i := 0; i < 1000; i++ {        z -= (z*z - x) / (2 * x)    }    return z}

In this way, my application package directory and code have been created, Note: It is generally recommended that the name and directory names of the packages remain consistent.

Custom package Installation

We have built our own application package, how to compile and install it? There are two ways to install

  • Go to the corresponding app package directory and then go install
  • Execute the following code in any directory go install MyMath

After we perform the installation, we can enter the following directory (\ ({goos}_\){goarch} and OS-related, like the blogger's Windows 64-bit is WINDOWS_AMD64)

cd $GOPATH/pkg/${GOOS}_${GOARCH}

You can see the following mymath.a file

This. A file is the application package, so how do we make the call?

Custom package Calls

Next we create a new executable application to invoke this application package, as in the previous steps, we create a new directory under the SRC directory Mathapp

cd %GOPATH%/srcmkdir mathapp

Then create a main.go file and add the following:

package mainimport (    "mymath"    "fmt")func main() {    fmt.Printf("Hello, world.  Sqrt(2) = %v\n", mymath.Sqrt(2))}

We can see that the package that we define this time is Main,import, which is called MyMath, which is the package we customized earlier.

It is necessary to note that if it is a multi-level directory, the import into a multi-level directory, if you have more than Gopath, is also the same, go will automatically find in multiple $gopath/src.

How do we compile our new Mathapp program? Go to the app directory and execute go build, then a Mathapp executable is generated under the directory, and the. exe file is generated by default under Windows.

We execute the mathapp.exe file and discover that it outputs the following content

Hello, world.  Sqrt(2) = 1.414213562373095

Remote Package

Get Remote Package

Go language has a tool to get the remote package is go get, now go get support most open source community (for example: GitHub, Googlecode, BitBucket, Launchpad)

go get github.com/astaxie/beedb

The go get-u parameter automatically updates the package and automatically gets the other third-party packages that the package relies on when go get

Through this command can obtain the corresponding source code, the corresponding open source platform using different source control tools, such as GitHub using Git, Googlecode use HG, so to obtain these sources, you must first install the corresponding source control tools.

The code obtained by above in our local source code is structured as follows

$GOPATH  src   |--github.com          |-astaxie              |-beedb   pkg    |--相应平台         |-github.com               |--astaxie                    |beedb.a

Go get is essentially understood as the first step is to go through the Source Code tool, clone, below SRC, and then run

Calls to remote packages

How to use a remote package in your code is as simple as using a local package, as long as you import the appropriate path at the beginning

import "github.com/astaxie/beedb"

Again: The bin directory is stored under the compiled executable file, pkg is stored under the application package, SRC is saved under the application source code.

Welcome to the Garden of the big men to correct mistakes, common progress. If you feel that the article is helpful, you can click "Recommend" in the lower right corner. Your encouragement is the author's greatest motivation to keep writing!

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.