How to Write Go code study notes

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

The article "How to Write Go code" is golang.org official document, the address is https://golang.org/doc/code.html.
The following is a study note.
The article mainly describes these aspects of the content:

    • Code structure & environment variable settings for Go
    • HelloWorld Sample Program
    • Stringutil Sample Library
    • Unit Test
    • Extended data (Follow-up learning)

1, Go Code organization structure & environment variable setting

The Go Code organization follows the following rules:

    • Generally there is only one workspace
    • There are multiple warehouses under a workspace, which are subordinate to different version control tools
    • Multiple package per warehouse
    • Each package is a stand-alone directory that contains multiple source files
    • The catalog of the package determines the import path

Environment variables need to be setGOPATH

2. HelloWorld Sample Program

    • If you go install do not specify parameters, you need to be in hello.go your directory;
    • If you set parameters, in any directory can be, parameters must be go install -github.com/user/hello ;

Same below

3. Stringutil Sample Library

    • go buildParameters above;
    • It is worth noting that the go build output is not generated if it is desired stringutil.a to be executed go install ;
$ go build

This won ' t produce an output file. To doing, you must use go install, which places the package object inside the pkg directory of the workspace.

4. Unit Test

Unit test is not the same as C + +, too wife advanced!
Just write the test function and call the test command on the line.

5, extension of information (follow-up learning) "very important"

See effective go for tips on writing clear, idiomatic go code.
Take A tour of Go to learn the language proper.
Visit the documentation page for a set of in-depth articles about the Go language and its libraries and tools.

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.