Fundamentals 8 min read

Master Go Operators and Control Flow: A Practical Guide

This tutorial introduces Go's fundamental operators—including arithmetic, relational, logical, bitwise, and assignment types—and explains the language's control flow constructs such as if, switch, for loops, and goto, complete with clear code examples for each concept.

Python Crawling & Data Mining
Python Crawling & Data Mining
Python Crawling & Data Mining
Master Go Operators and Control Flow: A Practical Guide

Go Operators Overview

This article continues the Go language basics series, focusing on operators and control flow.

Arithmetic Operators

+

: addition -: subtraction *: multiplication /: division %: remainder ++: increment --: decrement

Relational Operators

==

: equal !=: not equal >: greater than >=: greater than or equal <: less than <=: less than or equal

Logical Operators

&&

: logical AND ||: logical OR !: logical NOT

Bitwise Operators

&

: bitwise AND |: bitwise OR ^: bitwise XOR <<: left shift >>: right shift

Assignment Operators

=

: simple assignment +=: add then assign -=: subtract then assign *=: multiply then assign /=: divide then assign %=: remainder then assign <<=: left shift then assign >>=: right shift then assign &=: bitwise AND then assign |=: bitwise OR then assign ^=: bitwise XOR then assign

Control Flow

Go provides the following control structures:

if (including else if and else)

switch case

for (standard, infinite, range)

goto (use with caution)

If Statement

Syntax:

if condition {
    // statements
} else if otherCondition {
    // statements
} else {
    // statements
}

Example:

package main

import "fmt"

func main() {
    var gender = "男"
    if gender == "男" {
        fmt.Println("男")
    } else if gender == "女" {
        fmt.Println("女")
    } else {
        fmt.Println("啥都不是???")
    }
}

Switch Case

Switch works like a series of if statements but is more concise.

package main

import "fmt"

func main() {
    var week = 3
    switch week {
    case 1:
        fmt.Println("周一")
    case 2:
        fmt.Println("周二")
    case 3:
        fmt.Println("周三")
    default:
        fmt.Println("未知")
    }
}

For Loop

Standard for loop:

for i := 0; i < 10; i++ {
    fmt.Println(i)
}

Infinite loop (use carefully):

for {
    fmt.Println("666")
}

Range loop for collections:

package main

import "fmt"

func main() {
    var student_list = [...]string{"张三", "李四", "王五"}
    for index, v := range student_list {
        fmt.Println(index, v)
    }
}

Goto

Goto jumps to a labeled statement; overuse can make code hard to follow.

package main

import "fmt"

func main() {
    for i := 0; i < 10; i++ {
        for j := 0; j < 10; j++ {
            if j == 2 {
                goto breakTag
            }
            fmt.Printf("%v-%v
", i, j)
        }
    }
breakTag:
    fmt.Println("结束for循环")
}

Summary

This article covered Go's basic operators and control flow structures, providing explanations and runnable code examples to help beginners write correct Go programs.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendprogrammingGoTutorialoperatorsControl Flow
Python Crawling & Data Mining
Written by

Python Crawling & Data Mining

Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.