Go Language Basic grammar exercises (1)

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

1. For range structure

In the go language, the loop structure has only one keyword for, but its loop mode is still diversified.

For init;condition;post{

This is the standard for loop structure in C, with initial values, conditions, and steps.

For condition{

This is the analog C while loop, which needs to change the loop variable in the loop body

for{

This is the infinite loop, which is equivalent to the for in C (;;)

The go language also has a specific structure, for range

Package Mainimport "FMT" Func Main () {    language:=[]string{"C", "C + +", "Python", "Go", "Java"} for    key,value:= Range language{        FMT. Printf ("%d%s\n", Key,value)    }}

The For range structure returns the index and value, and the result is as follows:

2. VAR () define Variables

You can use Var to define different types of variables in the Go language

Package Mainimport "FMT" var (    a int = ten    b float32 = 3.1415926    c bool = True    d []int = []int{1,2,3,4,5}) Fun C Main () {     fmt. Printf ("%T%v\n", a,a)     FMT. Printf ("%T%v\n", b,b)     FMT. Printf ("%T%v\n", c,c)     FMT. Printf ("%T%v\n", d,d)    }

Notice how the array is defined,%t the type of the output variable,%v the value of the output variable, and run the result as follows:

For variables that do not have an assignment, the go language is initialized to 0,false, or an empty string.

3. Type conversion

Package Mainimport "FMT" Func Main () {     var a int = 5     var b float32 = 9.9     var c float32 = float32 (a)     var d int = Int (b)     FMT. Printf ("%v%v\n", C,d)}

The T (v) statement converts the type of the V variable to type T, and the result is as follows:

This syntax is similar to the type in Python, and the decimal place is removed when floating-point type is rounded.

4, the definition of constants

Package Mainimport "FMT" Func Main () {     const A = 10}
defined by the Const keyword and cannot be defined by: =


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.