Go Language Tour: Package

Source: Internet
Author: User

Each Go program is made up of some packages.

Original address: Https://golang-book.readthedocs.io
Welcome to our public number: Appetizer programming (Coding-fan)

The program main starts execution from the package.

package mainimport (    "fmt"    "math/rand")func main() {    fmt.Println("My favorite number is", rand.Intn(10))}

In this program, import two packages via import, fmt and math/rand (package path).

By convention, the package name is the same as the last part of the package path. For example, math/rand the source files in the package package rand begin with a statement.

Import statement

Go introduces the package through the import statement and is used in the code.

importThere are two different ways to write a statement, and the above example is one of them- bulk import ; the second is divided into multiple statements:

import "fmt"import "math"

Although there is no real difference between the two styles, it is recommended to use bulk notation , which is the best style .

Name Export

In the Go language, names beginning with uppercase letters are exported ( exported ). An example Pizza is an export name, which math is also in the package Pi .

package main import (     "fmt"     "math" ) func main() {     fmt.Println(math.Pi) }

Instead, pizza and pi because it is not capitalized, it is not exported.

After a package is imported, it can only be referenced to the exported name. Any other non-exported name outside the package is not accessible (not visible).

Next

In the next section, let's take a look at the Go language function.

Subscribe to the update For more information, please follow our public number:

Vegetable Learning Programming

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.