Skip to content

Crack SDE

Most of the content are generated by AI, with human being reviewed, edited, and revised

Menu
  • Home
  • Daily English Story
  • Tech Interviews
  • Cloud Native
  • DevOps
  • Artificial Intelligence
Menu

Check data type in golang

Posted on 10/11/202311/14/2023 by user

In Go (Golang), checking the data type of a variable can be done in several ways, depending on the context and what information you need about the data type. Here are some common methods to check data types:

1. Using Reflection with reflect.TypeOf

The reflect package provides a TypeOf function that returns the reflection Type of the value in the interface{}. This is a common way to check the data type of a variable.

package main

import (
    "fmt"
    "reflect"
)

func main() {
    var x float64 = 3.4
    fmt.Println("type:", reflect.TypeOf(x))
}

2. Type Assertion

Type assertion is used primarily with interfaces. It allows you to check if an interface contains a specific type.

package main

import (
    "fmt"
)

func main() {
    var i interface{} = "Hello"

    s, ok := i.(string)
    fmt.Println(s, ok)

    f, ok := i.(float64)
    fmt.Println(f, ok)

    if s, ok := i.(string); ok {
        fmt.Println(s)
    } else {
        fmt.Println("Value is not a string")
    }
}

3. Type Switch

A type switch is a construct that allows several type assertions in series. It’s useful when you want to perform different actions based on the concrete type of an interface variable.

package main

import (
    "fmt"
)

func do(i interface{}) {
    switch v := i.(type) {
    case int:
        fmt.Printf("Twice %v is %v\n", v, v*2)
    case string:
        fmt.Printf("%q is %v bytes long\n", v, len(v))
    default:
        fmt.Printf("I don't know about type %T!\n", v)
    }
}

func main() {
    do(21)
    do("hello")
    do(true)
}

4. Comparing with reflect.DeepEqual

Sometimes, you might want to check if two variables are of the same type. reflect.DeepEqual can be used for this, although it’s more commonly used to compare values.

package main

import (
    "fmt"
    "reflect"
)

func main() {
    a := 42
    b := "hello"

    fmt.Println("Same type:", reflect.DeepEqual(a, b))
}

Each of these methods serves different purposes and can be used depending on your specific needs. Remember that using reflection (reflect.TypeOf) can have performance implications and should be used judiciously in performance-critical code.

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X

Related

Recent Posts

  • LC#622 Design Circular Queue
  • Started with OpenTelemetry in Go
  • How Prometheus scrap works, and how to find the target node and get the metrics files
  • How to collect metrics of container, pods, node and cluster in k8s?
  • LC#200 island problem

Recent Comments

  1. another user on A Journey of Resilience

Archives

  • May 2025
  • April 2025
  • February 2025
  • July 2024
  • April 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • September 2023
  • August 2023
  • June 2023
  • May 2023

Categories

  • Artificial Intelligence
  • Cloud Computing
  • Cloud Native
  • Daily English Story
  • Database
  • DevOps
  • Golang
  • Java
  • Leetcode
  • Startups
  • Tech Interviews
©2025 Crack SDE | Design: Newspaperly WordPress Theme
Manage Cookie Consent
To provide the best experiences, we use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us to process data such as browsing behavior or unique IDs on this site. Not consenting or withdrawing consent, may adversely affect certain features and functions.
Functional Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
Manage options Manage services Manage {vendor_count} vendors Read more about these purposes
View preferences
{title} {title} {title}