Home >Backend Development >Golang >Example analysis of how golang operates

Example analysis of how golang operates

PHPz
PHPzOriginal
2023-04-11 09:10:38754browse

Go language is a programming language inspired by C language and Python language. It is different from many other programming languages ​​in programming paradigm and syntax. In the Go language, operations also have some special rules and characteristics. This article will introduce these contents in detail.

1. Operators in Go language

The operators in Go language are similar to those in other languages, mainly including:

  1. Arithmetic operators: , -, * ,/,%;
  2. Relational operators: ==,!=,>,<,>=,<=;
  3. Logical operators: &&, ||, !;
  4. Bit operators: &, |, ^, <<, >>;
  5. Assignment operators: =, =, -=, *=, /= , %=, &=, |=, ^=, <<=, >>=;
  6. Other operators: &, *, <-.

2. Numerical operations

  1. Integer operations

Go language supports various types of integer operations, including addition ( ), subtraction Remove (-), multiply by (*), divide by (/), find the remainder (%), etc. You can use the following code to sum two integers:

package main

import "fmt"

func main() {
    var a int = 21
    var b int = 10
    var c int

    c = a + b
    fmt.Printf("a + b 的值为: %d\n", c )

    c = a - b
    fmt.Printf("a - b 的值为:%d\n", c )

    c = a * b
    fmt.Printf("a * b 的值为:%d\n", c )

    c = a / b
    fmt.Printf("a / b 的值为:%d\n", c )

    c = a % b
    fmt.Printf("a %% b 的值为:%d\n", c )
}
  1. Operations of floating point numbers

Go language also supports floating point operations, including addition, subtraction, Multiply, divide and find remainders etc. We can use the following code to verify:

package main

import "fmt"

func main() {
    var a float32 = 10.0
    var b float32 = 3.0

    fmt.Printf("a + b = %f\n",a+b)
    fmt.Printf("a - b = %f\n",a-b)
    fmt.Printf("a * b = %f\n",a*b)
    fmt.Printf("a / b = %f\n",a/b)
    fmt.Printf("a %% b = %f\n",a%b)
}

3. Logical operations

The logical operators in Go language mainly include and (&&), or (||) and not (!) . The following is a simple example:

package main

import "fmt"

func main() {
    var a bool = true
    var b bool = false

    if (a && b) {
        fmt.Println("a && b 是 true")
    } else {
        fmt.Println("a && b 是 false")
    }

    if (a || b) {
        fmt.Println("a || b 是 true")
    } else {
        fmt.Println("a || b 是 false")
    }

    if (!a) {
        fmt.Println("!a 是 true")
    } else {
        fmt.Println("!a 是 false")
    }
}

4. Other operations

There are some other operators in the Go language, as follows:

  1. Get the pointer address Operator: &
package main

import "fmt"

func main() {
    a := 10
    fmt.Printf("a 变量的地址是:%x\n", &a )
}
  1. Get the value of the object pointed to by the pointer Operator: *
package main

import "fmt"

func main() {
    var a int = 10
    var ip *int

    ip = &a
    fmt.Printf("变量a= %d\n", a)
    fmt.Printf("指针变量 *ip= %d\n", *ip)
}
  1. Channel send/receive operator: < ;-
package main

import "fmt"

func main() {
    var ch chan int
    ch = make(chan int)

    go func() {
        ch <- 2
    }()

    var value int
    value = <-ch
    fmt.Println(value)
}

Summary

This article introduces the operation rules and features in Go language, involving integers, floating point numbers, logical operators, bit operators, assignment operators and others operators, etc. Understanding these contents will help us better understand and remember the grammar and usage of Go language.

The above is the detailed content of Example analysis of how golang operates. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn