52. Go - Type Assertions for Interfaces

Assertion allows accessing exact types from an interface.

Using parenthesis and the type inside.


Example:

package main

import "fmt"

func main() {
    var jar interface{} = 2000

    x := jar.(int)

    fmt.Println(x)

    y, err := jar.(string)
    if err {
        fmt.Println("STRING FOUND: ", y)
    } else {
        fmt.Println("STRING NOT FOUND")
    }
}

// using double variables allow checking if the type exists in an interface.

// if true, else 

Comments

Popular posts from this blog

2. FreeCodeCamp - Dynamic Programming - Learn to Solve Algorithmic Problems & Coding Challenges

20. Data Analytics - Analyze Data to Answer Questions - Week 1

3. Algorithms - Selection Sort