Table of Contents
Basic Arithmetic: , -, *, /, %
Increment and Decrement: and --
Mixing Types? Be Careful!
Home Backend Development Golang How do I use arithmetic operators in Go ( , -, *, /, %, , --)?

How do I use arithmetic operators in Go ( , -, *, /, %, , --)?

Jun 21, 2025 am 12:54 AM
go language arithmetic operators

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.

How do I use arithmetic operators in Go ( , -, *, /, %, , --)?

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 addition
  • - 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!

Statement of this Website
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

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1512
276
How to solve the user_id type conversion problem when using Redis Stream to implement message queues in Go language? How to solve the user_id type conversion problem when using Redis Stream to implement message queues in Go language? Apr 02, 2025 pm 04:54 PM

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? What should I do if the custom structure labels in GoLand are not displayed? Apr 02, 2025 pm 05:09 PM

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? Do I need to install an Oracle client when connecting to an Oracle database using Go? Apr 02, 2025 pm 03:48 PM

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 provided by well-known open source projects? Which libraries in Go are developed by large companies or provided by well-known open source projects? Apr 02, 2025 pm 04:12 PM

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, ...

In Go programming, how to correctly manage the connection and release resources between Mysql and Redis? In Go programming, how to correctly manage the connection and release resources between Mysql and Redis? Apr 02, 2025 pm 05:03 PM

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

centos postgresql resource monitoring centos postgresql resource monitoring Apr 14, 2025 pm 05:57 PM

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

Go vs. Other Languages: A Comparative Analysis Go vs. Other Languages: A Comparative Analysis Apr 28, 2025 am 12:17 AM

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

Why is it necessary to pass pointers when using Go and viper libraries? Why is it necessary to pass pointers when using Go and viper libraries? Apr 02, 2025 pm 04:00 PM

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...

See all articles