0%

GO_Learning_Fundamental_Integers

Control structures

In Go language, there is only if , for , switch as control structure while no do, while loop.

  • Same with other language: break,continue statements is the same as others
  • Different :
    • in switch structure, type is optional label
    • select

Syntax in Go: No need parentheses while the body must always be brace-delimited.

if

  • Ordinary syntan: Writting simple if in multiple lines.

    1
    2
    3
    if x > 0 {
    return y
    }
  • Intersting syntax: if with initialization statement

    1
    2
    3
    4
    if err := file.Chmod(0664); err != nil {
    log.Print(err)
    return err
    }

for

There are three forms in for syntaxs:

1
2
3
4
5
6
7
8
// Like a C for
for init; condition; post { }

// Like a C while
for condition { }

// Like a C for(;;)
for { }

for with range:

range statements is a good way to loop array, slice, map, channel, the syntax is :

1
2
3
for key, value := range oldMap {
newMap[key] = value
}

If you only need the first item in the range (the key or index), drop the second:

1
2
3
4
5
for key := range m {
if key.expired() {
delete(m, key)
}
}

If you only need the second item in the range (the value), use the blank identifier, an underscore, to discard the first:

1
2
3
4
sum := 0
for _, value := range array {
sum += value
}
  • Range does more work for string:Breaking out individual Unicode code points by parsing the UTF-8. Erroneous encodings consume one byte and produce the replacement rune U+FFFD.
1
2
3
4
5
6
7
8
for pos, char := range "日本\x80語" { // \x80 is an illegal UTF-8 encoding
fmt.Printf("character %#U starts at byte position %d\n", char, pos)
}

//character U+65E5 '日' starts at byte position 0
//character U+672C '本' starts at byte position 3
//character U+FFFD '�' starts at byte position 6
//character U+8A9E '語' starts at byte position 7

switch

switch has the same mwaning as other language. In go language, if the switch has no expression it switches on true. It’s therefore possible—and idiomatic—to write an if-else-if-else chain as a switch.

1
2
3
4
5
6
7
8
9
10
11
func unhex(c byte) byte {
switch {
case '0' <= c && c <= '9':
return c - '0'
case 'a' <= c && c <= 'f':
return c - 'a' + 10
case 'A' <= c && c <= 'F':
return c - 'A' + 10
}
return 0
}

There is no automatic fall through, but cases can be presented in comma-separated lists.

1
2
3
4
5
6
7
func shouldEscape(c byte) bool {
switch c {
case ' ', '?', '&', '=', '#', '+', '%':
return true
}
return false
}

Type switch

A switch can also be used to discover the dynamic type of an interface variable. Such a type switch uses the syntax of a type assertion with the keyword type inside the parentheses. If the switch declares a variable in the expression, the variable will have the corresponding type in each clause. It’s also idiomatic to reuse the name in such cases, in effect declaring a new variable with the same name but a different type in each case.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
var t interface{}
t = functionOfSomeType()
switch t := t.(type) {
default:
fmt.Printf("unexpected type %T\n", t) // %T prints whatever type t has
case bool:
fmt.Printf("boolean %t\n", t) // t has type bool
case int:
fmt.Printf("integer %d\n", t) // t has type int
case *bool:
fmt.Printf("pointer to boolean %t\n", *t) // t has type *bool
case *int:
fmt.Printf("pointer to integer %d\n", *t) // t has type *int
}