A Tour of Go:basics 1

Source: Internet
Author: User

Packages, variables and functionspackages

In packages, the name that starts with a capital letter is exported name, and when the import package is in, only exported name can be accessed externally.

Functions

A continuous parameter of the same type can be specified only at the end of the type.
A function can have more than one return value.

func swap(x, y string) (string, string) {        return y, x}

Go supports the return value with Name:

    • When a function is defined, it is defined to return the variable name, and the return variable is manipulated within the function.
    • Returned with the naked return statement.
func split(sum int) (x, y int) {        x = sum * 4 / 9        y = sum - x        return}

Note: It is recommended to use this only in short functions, because it can easily affect readability.

Variables

The var keyword defines the variable.
You can omit the type when there is an initial value.
Tips and points of attention:

    • Within a function, you can use the: = symbol to replace the variable definition with the initial value.
    • But outside of the function, all statements must start with a keyword, so you cannot use the: = symbol.
Basic types
boolstringint  int8  int16  int32  int64uint uint8 uint16 uint32 uint64 uintptrbyte // alias for uint8rune // alias for int32        // represents a Unicode code pointfloat32 float64complex64 complex128

Skills:

    • Both Var and import can declare multiple packages or variables in parentheses.
    • It is recommended that you do not need to specify a size or sign if you have no special needs.

variable is defined, if no initial value is specified, the default value of the corresponding type is assigned.

    • Numeric type:0
    • Bool:false
    • String: ""

The expression T (V) indicates that the value V is converted to type T:

var i = 10var f = float64(i)

Note: Unlike the C language, go must be explicitly converted.

The constant definition replaces var with the Const keyword, but cannot use the: = symbol.

Questions
    1. Numeric constants is high-precision values.

A Tour of Go:basics 1

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.