Go - (1) Begining

A bit about Go Any interpreted language is slower than Go because it is compiled. High execution and compilation speed. Go is faster than interpreted or VM-powered languages like Python, JS, PHP, Ruby, and Java Go is slower in execution speed than compiled languages like C, C++, and Rust. Go is slower in the above case because of its automated memory management (Go runtime). Compilation: converting human readable code into machine readable code (to binary). Go does compile work upfront. When you write a Go file and build it, it generates an executable that can be directly run on a machine. But an interpreted language converts the code as we run it. Go is a static-typed language. If you initially set a variable as a string, you can't later change it to another data type. Go has automated memory management. When Go code is compiled, you get one binary (an executable program) with a runtime in it. That runtime is used for garbage collection and automated memory management. Memory efficient languages: Java

Apr 12, 2025 - 13:46
 0
Go - (1) Begining

A bit about Go

  • Any interpreted language is slower than Go because it is compiled.
  • High execution and compilation speed.
  • Go is faster than interpreted or VM-powered languages like Python, JS, PHP, Ruby, and Java
  • Go is slower in execution speed than compiled languages like C, C++, and Rust.
  • Go is slower in the above case because of its automated memory management (Go runtime).

Compilation: converting human readable code into machine readable code (to binary).

  • Go does compile work upfront. When you write a Go file and build it, it generates an executable that can be directly run on a machine. But an interpreted language converts the code as we run it.
  • Go is a static-typed language. If you initially set a variable as a string, you can't later change it to another data type.
  • Go has automated memory management. When Go code is compiled, you get one binary (an executable program) with a runtime in it. That runtime is used for garbage collection and automated memory management.
  • Memory efficient languages: Java <<< Go < Rust
  • One of the purposes of Go runtime is to clean up unused memory.

Numerics in Go

Numbers fall into four categories when it comes to

  1. integers: whole numbers
  2. unsigned integers (units): positive numbers
  3. floats
  4. complex numbers: imaginary numbers

The size of the numbers should also be considered. For example,e uint16 is twice the size of uint8. So you should use the data type that matches your requirement. For example, 255 is the largest number you can store in a uint8.

  • There is another data type named byte which is usually used in scenarios like marshaling a JSON object. It is also an alias for ùint8` because a byte is just 8 binary digits.
  • rune is an alias for int32

Declaring variables

  • Golang has := operator which is used for variable initialization
  • Ex: name := "". Here, there is no need to specify the datatype of name because Go identifies it as a string.

If you want to print the type of a variable instead of its value use %T.

  • Ex: fmt.Printf("The type of name variable is %T\n", name)

You can also declare multiple variables in the same line.

  • Ex: name, age := "Edward", 17

Number conversions

We can also convert numbers between different number types. Ex:

numInInt := 88
numInFloat := float64(numInInt)

But converting it the other way (float to int) is tricky. Because if the number is 88.8, we will lose 0.8 when converted to int.

  • The size of a type (ex: int64) is indicated in bits.

In JS const means you can't reassign to a variable. But you can compute the variable value at runtime. In Go, constants must be known at compile time (before running the program). Ex:

const Greeting = "Hello World!" // ✅ correct
const RandomNumber = rand.Intn(10) // ❌ Compile-time error because of rand.Intn(10) is only known at run time

Formatting strings

Formatting strings in Go is a bit unfriendly. We use the fmt package for that which includes functions like Printf (print a formatted string directly to standard out) and Sprintf (returns formatted string as a value). Below are several formatting verbs in Go.

  • %s: for string
  • %d: for int in decimal
  • %f: for decimal (ex: 8.254)
  • %t: for bool
  • %v: the default formatted

Ex: fmt.Printf("It costs %.2f", 34.746) gives "It costs 34.75".

Conditionals in Go

  • The syntax is given below, the execution is pretty much the same as in other languages.

if marks >= 75 {
fmt.Println("You got an A")
} else if marks >= 45 {
fmt.Println("You got an Pass")
} else {
fmt.Println("You failed.")
}

One of the cleaning code hacks in Go, when a variable is only used for a conditional check is given below

if err := getUsername(); err != nil {
return fmt.Errorf("err", err)
}