👊
Go tutorial for Pythoner
  • Introduction
  • Install and Run Go
  • The structure of Go
    • First time you meet him
    • Functions
    • Return multiple results
    • Logic Control
    • Break loop and enumerate
    • List (Array or Slice)
    • Dict (Map)
    • Struct
    • Class (Methods)
    • Packages
  • Features of Go
  • Go modules
  • Web Server
    • Serving static files
    • A simple webserver
  • Go for Android
  • Where to go next
Powered by GitBook
On this page
  • Codes:
  • Output:
  • Conclusion:
  • Codes:
  • Conclusion:
  • We haven't finished yet. The most important control method is if and else.
  • It's definitely inherited from C++, I can feel it.
  • I must say, compare to Python, you have to write more codes to reach the same goal.

Was this helpful?

  1. The structure of Go

Logic Control

Codes:

package main

import "fmt"

func main() {
    i := 1
    for i <= 10 {
        fmt.Println(i)
        i = i + 1
    }
}

Output:

1
2
3
4
5
6
7
8
9
10

Conclusion:

The for in there, is like Python's while. In other words, is just like if, for example: if i <= 10: do things.

Codes:

package main

import "fmt"

func main() {
    for i := 1; i <= 10; i++ {
        fmt.Println(i)
    }
}

Conclusion:

This structure is picked up from C++. You can understand it in this way: for i == 1, if i <= 10, running following part of codes, then i = i + 1. (for == 对于、基于)

We haven't finished yet. The most important control method is if and else.

It's definitely inherited from C++, I can feel it.

package main

import "fmt"

func main() {
    i := 1
    for i <= 10 {
        if i % 2 == 0 {
            fmt.Println(i, "even")
        } else if i == 7 {
            fmt.Println("I got", i, "!!!")
        } else {
            fmt.Println(i, "odd")
        }
        i = i + 1
    }
}

I must say, compare to Python, you have to write more codes to reach the same goal.

PreviousReturn multiple resultsNextBreak loop and enumerate

Last updated 4 years ago

Was this helpful?