Go Part I: Variables, constants, and enumeration types

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

GoIn the first article, I will refine the basic points that are used frequently, and for those children who have just begun to get started Go , the foundation is not solid enough to refer to Go the basic usage of this article for quick review.

Series Finishing:

    • Go Part II: Branch statements, functions

If you are interested in the Go language itself, you can read my translation of the advantages, disadvantages and repulsive design of the go language.

Variable definition

// 默认为零值var a stringvar b intvar a, b string // 声明变量值var e string = "abc"// 变量属于同一类型,可只写一次类型var c, d int = 3, 4// 推论类型var f, g, h, i = 3, 4, true, "def"f, g, h, i := 3, 4, true, "def"// 括号写法var (    aa = 3    ss = "kkk"    bb = true)

Built-in variable type

    • BOOL, string
    • (u) int, (U) int8, (U) int16, (U) Int32, (U) Int64, uintptr
    • Byte, Rune
    • float32, Float64, complex64, complex128

Complex is a plural type

// 不用1i,程序会将 i 识别为变量fmt.Printf("%.3f\n", cmplx.Exp(1i*math.Pi) + 1)

Forcing type conversions

var a, b int = 3, 4// math.Sqrt 参数和返回值都是  float64c := int(math.Sqrt(float64(a *a + b * b)))

Definition of constants

const a, b = 3, 4const (    filename = "abc.txt"    a, b = 3, 4)// const 数值可以作为任意的类型使用,所以此处 a, b变量不需要手动转换为 float64c := int(mant.Sqrt(a*a + b*b))

Self-Increment enumeration type

const (    javascript = iota    c    python    _    php)const (    b = 1 << (10 * iota)    kb    mb    gb    tb    pb)
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.