Skip to content

Operators in Go

We used some operators so far in our code examples, like =, := and <.

Let’s talk a bit more about them.

We have assignment operators = and := we use to declare and initialize variables:

var a = 1

b := 1

We have comparison operators == and != that take 2 arguments and return a boolean

var num = 1
num == 1 //true
num != 1 //false

and <, <=, >, >=:

var num = 1
num > 1 //false
num >= 1 //true
num < 1 //false
num <= 1 //true

We have binary (require two arguments) arithmetic operators, like +, -, *, /, %.

1 + 1 //2
1 - 1 //0
1 * 2 //2
2 / 2 //1
2 % 2 //0

+ can also join strings:

"a" + "b" //"ab"

We have unary operators ++ and -- to increment or decrement a number:

var num = 1
num++ // num == 2
num-- // num == 1

Note that unlike C or JavaScript we can’t prepend them to a number like ++num. Also, the operation does not return any value.

We have boolean operators that help us with making decisions based on true and false values: &&, || and !

true && true  //true
true && false //false
true || false //true
false || false //false
!true  //false
!false //true

Those are the main ones.


→ Get my Go Handbook

→ I wrote 17 books to help you become a better developer:

  • C Handbook
  • Command Line Handbook
  • CSS Handbook
  • Express Handbook
  • Git Cheat Sheet
  • Go Handbook
  • HTML Handbook
  • JS Handbook
  • Laravel Handbook
  • Next.js Handbook
  • Node.js Handbook
  • PHP Handbook
  • Python Handbook
  • React Handbook
  • SQL Handbook
  • Svelte Handbook
  • Swift Handbook
...download them all now!

Also, JOIN MY CODING BOOTCAMP, an amazing cohort course that will be a huge step up in your coding career - covering React, Next.js - next edition February 2025

Bootcamp 2025

Join the waiting list