How do I use arithmetic operators in Go ( , -, *, /, %, , --)?
The methods of using arithmetic operators in Go language include: 1. Basic operators, -, *, /, % are used for addition, subtraction, multiplication, division and separation, and taking remainder. The result of integer division is an integer, and the division of negative numbers is rounded to zero, and the remainder only supports integers; 2. Self-increase and self-decrease--can only act on variables as independent statements and cannot be used in expressions; 3. Mixed type operations require explicit conversion of types, and cannot directly operate on different types. For example, int and float64 must be converted to the same type first.
Using arithmetic operators in Go is straightforward, and they work pretty much like you'd expect if you've used other C-style languages. Let's go over how to use each of them effectively.
Basic Arithmetic: , -, *, /, %
These are the standard math operations you'll use most often:
-
-
-
for subtraction -
*
for multiplication -
/
for division -
%
for modulus (remainder after division)
Here's a quick example:
a := 10 b := 3 fmt.Println(ab) // 13 fmt.Println(a - b) // 7 fmt.Println(a * b) // 30 fmt.Println(a / b) // 3 (since both are integers) fmt.Println(a % b) // 1
A few things to note:
- If both operators are integers, the result will also be an integer.
- Division with negative numbers rounds toward zero.
- Modulus works only with integers in Go (unlike some other languages).
Increment and Decrement: and --
Go uses
to increment by 1 and --
to decline by 1. But unlike in C or Java, these can't be used in expressions — they're statements only.
For example:
i := 5 i fmt.Println(i) // 6 j := 10 j-- fmt.Println(j) // 9
You can't do something like this:
x := i // ❌ Compile error
So just remember:
- They only work on variables, not constants or expressions.
- They must appear on their own line as standalone operations.
Mixing Types? Be Careful!
Go doesn't allow mixing types automatically in arithmetic. For example, you can't add an int
and an float64
directly — you have to convert one to match the other.
var x int = 5 var y float64 = 2.5 // This won't compile: // fmt.Println(xy) // You need to convert: fmt.Println(float64(x) y) // OK
Same goes for different integer types like int8
, int16
, etc. Always make sure your types match before doing math.
That's the core of using arithmetic operators in Go. The rules are simple, but strict — especially around type conversion and where /- can be used.
The above is the detailed content of How do I use arithmetic operators in Go ( , -, *, /, %, , --)?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

The problem of using RedisStream to implement message queues in Go language is using Go language and Redis...

What should I do if the custom structure labels in GoLand are not displayed? When using GoLand for Go language development, many developers will encounter custom structure tags...

Do I need to install an Oracle client when connecting to an Oracle database using Go? When developing in Go, connecting to Oracle databases is a common requirement...

Which libraries in Go are developed by large companies or well-known open source projects? When programming in Go, developers often encounter some common needs, ...

Resource management in Go programming: Mysql and Redis connect and release in learning how to correctly manage resources, especially with databases and caches...

Detailed explanation of PostgreSQL database resource monitoring scheme under CentOS system This article introduces a variety of methods to monitor PostgreSQL database resources on CentOS system, helping you to discover and solve potential performance problems in a timely manner. 1. Use PostgreSQL built-in tools and views PostgreSQL comes with rich tools and views, which can be directly used for performance and status monitoring: pg_stat_activity: View the currently active connection and query information. pg_stat_statements: Collect SQL statement statistics and analyze query performance bottlenecks. pg_stat_database: provides database-level statistics, such as transaction count, cache hit

Goisastrongchoiceforprojectsneedingsimplicity,performance,andconcurrency,butitmaylackinadvancedfeaturesandecosystemmaturity.1)Go'ssyntaxissimpleandeasytolearn,leadingtofewerbugsandmoremaintainablecode,thoughitlacksfeatureslikemethodoverloading.2)Itpe

Go pointer syntax and addressing problems in the use of viper library When programming in Go language, it is crucial to understand the syntax and usage of pointers, especially in...
