Home >Backend Development >Golang >Example analysis of how golang operates
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:
2. Numerical 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 ) }
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:
package main import "fmt" func main() { a := 10 fmt.Printf("a 变量的地址是:%x\n", &a ) }
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) }
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!