Go - Maps

Intro Maps are somewhat similar to what other languages call “dictionaries” or “hashes”. A map maps keys to values. Here are some points to look at: Maps allows us to quickly access to an element/value using a unique key. Map keys must be unique because otherwise it can’t find the corresponding values/elements. The types of Map Keys and Values in Maps can be different. A Map Key must be a comparable type. [Read More]

Attaching Method to any type

  • We can attach methods to any type in Go. For e.g.

    // Basic Types
    int
    string
    float64
    
    // Bare Types
    array
    struct
    
    // -----------------------
    // Do not use "Pointer Receivers" with below types since they already carry a pointer with themselves.
    // i.e. slice, map, chan, func
    // -----------------------
    // Pointer Bearing Types
    slice
    map
    chan // Channels
    
    // We can also attach methods to:
    func
    

Go Conditionals - switch

Intro In addition to the switch keyword, a switch statement has cases. The switch statement switches on some case. Example - No break required Compared to other programming languages (such as C), Go’s switch-case statement doesn’t need explicit “break”, and not have fall-though characteristic. Take the following code as an example: package main import ( "fmt" ) func checkSwitch(val int) { switch val { case 0: case 1: fmt.Println("The value is: ", val) } } func main() { checkSwitch(0) checkSwitch(1) } The output is: [Read More]

Error handling intro

nil is a predeclared identifier like true, false, len(), int32, float64 etc. Since it is a predeclared identifier, it can be used anywhere without importing any package. nil value means that the value is not initialized yet. It is similar to following identifiers in other languages: null // JavaScript None // Python null // Java nil // Ruby The zero value of all pointer-based types in Go is nil. Following are the pointer-based types in Go: [Read More]

Error handling intro

Error is by far the most used interface in Go. lets have a look at it: type error interface { Error() string } //The error built-in interface type is the conventional interface for representing an error condition, with the nil value representing no error. So any type that implements the Error method is by default an error type. Some Examples: package main import "fmt" type MyError string func (me MyError) Error() string{ return string(me) //cast it back to string } type MyOtherError struct{ Code int Message string } func (me MyOtherError) Error() string{ return fmt. [Read More]

Go - Basic Data Type

Following are the basic data types in Go: Numeric // Integer Types uint8 // Unsigned 8-bit integers (0 to 255) uint16 // Unsigned 16-bit integers (0 to 65535) uint32 // Unsigned 32-bit integers (0 to 4294967295) uint64 // Unsigned 64-bit integers (0 to 18446744073709551615) int8 // Signed 8-bit integers (-128 to 127) int16 // Signed 16-bit integers (-32768 to 32767) int32 // Signed 32-bit integers (-2147483648 to 2147483647) int64 // Signed 64-bit integers (-9223372036854775808 to 9223372036854775807) // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ // Floating Types float32 // IEEE-754 32-bit floating-point numbers float64 // IEEE-754 64-bit floating-point numbers complex64 // Complex numbers with float32 real and imaginary parts complex128 // Complex numbers with float64 real and imaginary parts // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ // Other Numeric Types byte // same as uint8 rune // same as int32 uint // 32 or 64 bits int // same size as uint uintptr // an unsigned integer to store the uninterpreted bits of a pointer value Handling Overflows At compile time, a Go compiler can catch overflow errors. [Read More]

Go - Data Type Intro

There are following data types in Go:

  • Basic type: Numbers, strings, and booleans come under this category.
  • Aggregate type: Array and structs come under this category.
  • Reference type: Pointers, slices, maps, functions, and channels come under this * category.
  • Interface type

Go - Structs

Intro A struct is a collection of fields/properties. You can define new types as structs or interfaces. If you are coming from an object-oriented background, you can think of a struct to be a light class that supports composition but not inheritance. Structs are blueprints — They are fixed and created at compile-time, but struct values fill them in runtime.. It’s like a class in OOP languages. Groups related data in a single type. [Read More]

Go Pointers

Go has pointers, but no pointer arithmetic. Struct fields can be accessed through a struct pointer. The indirection through the pointer is transparent (you can directly call fields and methods on a pointer). Note that by default Go passes arguments by value (copying the arguments), if you want to pass the arguments by reference, you need to pass pointers (or use a structure using reference values like slices (Section 4.2) and maps (Section 4. [Read More]

Method with struct and interface

Methods enhance types with additional behavior. Methods of the type are called Method Set. To attach method to a type : // Syntax // “varName Type” is called a “receiver” func (varName Type) funcName() { // Code } // Example // “book” is a struct here func (b book) printBook() { fmt.Println(b.title, b.price) } - A `receiver` is nothing but method's `input parameters` written `before` a `method name`. - A `method` belongs to a `single type`. [Read More]