Go Programming - Full Course

Go Programming - Full Course

Go Programming Language: Basic Operators and Math Package

In this blog post, we will cover some of the basic operators in Go programming language and also explore the math package.

Basic Operators

Go has several basic operators that are similar to other programming languages. These include arithmetic operators, comparison operators, logical operators, assignment operators, and more.

Arithmetic Operators

Arithmetic operators are used for performing mathematical operations such as addition, subtraction, multiplication, division, etc.

  • Addition: +
  • Subtraction: -
  • Multiplication: *
  • Division: /

For example:

x := 10
y := 5

fmt.Println(x + y) // prints 15
fmt.Println(x - y) // prints 5
fmt.Println(x \* y) // prints 50
fmt.Println(x / y) // prints 2.0

Comparison Operators

Comparison operators are used for comparing two values.

  • Equal to: ==
  • Not equal to: !=
  • Greater than: >
  • Less than: <
  • Greater than or equal to: >=
  • Less than or equal to: <=

For example:

x := 10
y := 5

fmt.Println(x == y) // prints false
fmt.Println(x != y) // prints true
fmt.Println(x > y) // prints true
fmt.Println(x < y) // prints false

Logical Operators

Logical operators are used for combining conditional statements.

  • And: &&
  • Or: ||
  • Not: !

For example:

x := 10
y := 5

fmt.Println(x > 5 && y < 10) // prints true
fmt.Println(x > 5 || y < 10) // prints true
fmt.Println(!(x > 5)) // prints false

Assignment Operators

Assignment operators are used for assigning values to variables.

  • Assign: =
  • Add and assign: +=
  • Subtract and assign: -= etc.

For example:

x := 10
y := 5

x += y // x is now 15

Math Package

The math package in Go provides functions for mathematical operations such as calculating the minimum, maximum, square root, power, round, ceiling and floor of a number.

Importing the Math Package

To use the math package, you need to import it at the beginning of your program using the following statement:

import "math"

Using the Math Functions

Here are some examples of how to use the math functions:

  • math.Min(): Returns the minimum of two numbers.```go fmt.Println(math.Min(5, 10)) // prints 5
*   `math.Max()`: Returns the maximum of two numbers.

    ```go
fmt.Println(math.Max(5, 10)) // prints 10
  • math.Sqrt(): Returns the square root of a number.```go fmt.Println(math.Sqrt(16)) // prints 4.0
*   `math.Pow()`: Returns the power of two numbers.

    ```go
fmt.Println(math.Pow(2, 3)) // prints 8.0
  • math.Round(): Returns the nearest integer to a number.```go fmt.Println(math.Round(4.25)) // prints 4
*   `math.Ceil()`: Returns the smallest integer that is greater than or equal to a number.

    ```go
fmt.Println(math.Ceil(4.25)) // prints 5
  • math.Floor(): Returns the largest integer that is less than or equal to a number.```go fmt.Println(math.Floor(4.25)) // prints 4
**String to Integer Conversion**
--------------------------------

To convert a string to an integer, you can use the `strconv` package in Go.

go import "strconv"

func main() { s := "123" i, err := strconv.Atoi(s) if err != nil { fmt.Println(err) } else { fmt.Println(i) // prints 123 } } ```

This is a basic overview of the math package and some common operators in Go. You can find more information about these topics in the official Go documentation.


Want to create posts like this?

This entire article—title, structure, and text—was automatically generated from a video transcript using Matadata.ai.

Stop wasting hours writing show notes and blog posts. Turn your YouTube videos into a content empire in seconds.

Try Matadata for Free →