First time, how to write go code

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

Gopath and Workspace

The core principle of the go language is to keep it simple, and in terms of project construction, the source code and the build script are put together, and the structure is fixed, just like maven.

Add a go after path (Download the unpacked Go package) bin, so that you can freely use the GO command

Gopath's system environment variables, and ava_home are not the same for Java, to allow GO commands and other related tools to find the Go workspace

Gopath D:\home\user\ext;d:\home\user\mygo
can have multiple, semicolon-separated. The colon is separated from UNIX.

The structure of each workspace

    • srcContains Go source files,
    • pkgContains compiled package objects, and
    • binContains executable commands

Create these three folders.

Then you can write the go source file in Src,

Command and then execute the Go Install command


For the Sqrt.go file its file path is D:\workspacego\src\example\newmath

D:\workspacego is Gopath.

SRC is the source directory

Example is the name of the project

Newmath This is the package declaration in the Code

Package Newmath
SQRT returns an approximation to the square root of X.
Func Sqrt (x float64) float64 {
This is a terrible implementation.
Real code should import "math" and use MATH.SQRT.
Z: = 0.0
For I: = 0; i < 1000; i++ {
Z-= (z*z-x)/(2 * x)
}
Return Z
}

And another file Hello.go D:\workspacego\src\example\hello

Its package is main, which represents his main function, which is the entry for this project.

Unlike the top package to save the file directory name, if it is main, the file name is consistent with the directory name

It will be converted to an execution command

Package Main
Import (
"Example/newmath"
"FMT"
)
Func Main () {
Fmt.  Printf ("Hello, World.") SQRT (2) =%v\n ", Newmath. SQRT (2))
}


Go Install Example/hello
Compiling the installation command

bin/    Hello              # command executablepkg/    linux_amd64/         example/            newmath.a  # Package objectsrc/    example/        hello/            hello.go   # command source        newmath/            sqrt.go    # package source
An execution command is generated under the bin and is hello.exe under window

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.