Translate

Types and type assertions in Golang

Types and Type Assertions in Go

Types in Go

Go एक statically typed language है, जिसका मतलब है कि सभी variables की types compile-time पर ही निर्धारित हो जाती हैं। Go में built-in types के अलावा, आप custom types भी define कर सकते हैं।

1. Built-in Types

Go में कुछ common built-in types निम्नलिखित हैं:

Basic Types:
  • int, int8, int16, int32, int64: Signed integers
  • uint, uint8, uint16, uint32, uint64: Unsigned integers
  • float32, float64: Floating-point numbers
  • complex64, complex128: Complex numbers
  • bool: Boolean type (true or false)
  • string: String type (sequence of characters)
Composite Types:
  • array: Fixed-size sequence of elements of the same type
  • slice: Dynamically-sized array
  • map: Key-value pairs
  • struct: Composite type with named fields
  • pointer: Memory address of a value
  • function: Code block that can be invoked
  • interface: Collection of method signatures

2. Custom Types

Go में आप custom types भी define कर सकते हैं, जो कि existing types के आधार पर नए types create करने की सुविधा देते हैं।

Example: Custom Types
package main

import "fmt"

// Defining a custom type named 'Age' based on 'int'
type Age int

// Defining a custom type named 'Person'
type Person struct {
    Name string
    Age  Age
}

func main() {
    var myAge Age = 25
    fmt.Println("My Age:", myAge)

    p := Person{Name: "Alice", Age: myAge}
    fmt.Println("Person:", p)
}

Explanation:

  • type Age int: यह statement एक custom type Age define करता है, जो int type पर आधारित है।
  • type Person struct { ... }: यह statement एक custom type Person define करता है, जो एक struct है और इसमें Name और Age fields हैं।
  • Output: "My Age: 25" और "Person: {Alice 25}"।

Type Assertions in Go

Type assertions का उपयोग interfaces में stored values की underlying type को retrieve करने के लिए किया जाता है। यह तब उपयोगी होता है जब आप यह check करना चाहते हैं कि interface के अंदर कौन-सी specific type की value store की गई है।

1. Basic Type Assertion

Type assertion की basic syntax इस प्रकार होती है:

value, ok := interfaceVariable.(ConcreteType)
  • interfaceVariable.(ConcreteType): यह type assertion है जो check करता है कि क्या interfaceVariable की underlying type ConcreteType है।
  • value: यह type assertion successful होने पर ConcreteType में converted value को hold करता है।
  • ok: यह boolean value है जो true होती है अगर type assertion successful होती है, अन्यथा false।
Example: Basic Type Assertion
package main

import "fmt"

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

    // Performing type assertion
    s, ok := i.(string)
    if ok {
        fmt.Println("String value:", s)
    } else {
        fmt.Println("Not a string")
    }

    // Attempting a type assertion to a different type
    n, ok := i.(int)
    if ok {
        fmt.Println("Integer value:", n)
    } else {
        fmt.Println("Not an integer")
    }
}

Explanation:

  • s, ok := i.(string): यह type assertion check करता है कि i में stored value string type की है। Successful होने पर, ok true होगा और s में string value stored होगी।
  • n, ok := i.(int): यह type assertion check करता है कि क्या i में stored value int type की है। चूंकि यह unsuccessful है, इसलिए ok false होगा।
  • Output: "String value: Hello, Go!" और "Not an integer"।

2. Type Assertion Without Checking

अगर आप type assertion करते समय ok variable को check नहीं करना चाहते हैं, तो आप इसे बिना ok के भी कर सकते हैं। लेकिन अगर type assertion fail होती है, तो Go runtime पर panic होगा।

Example: Type Assertion Without Checking
package main

import "fmt"

func main() {
    var i interface{} = 42

    // Type assertion without checking
    n := i.(int)
    fmt.Println("Integer value:", n)

    // Attempting a type assertion to a different type (will panic)
    s := i.(string) // This will cause a panic
    fmt.Println("String value:", s)
}

Explanation:

  • n := i.(int): यह type assertion successful होती है क्योंकि i में int value stored है।
  • s := i.(string): यह type assertion fail होगी क्योंकि i में string value नहीं है, और यह runtime पर panic करेगा।

3. Type Switch

Type switch एक powerful feature है जो multiple type assertions को handle करने के लिए उपयोग किया जाता है। यह switch statement की तरह काम करता है, लेकिन यह values की type पर switch करता है।

Example: Type Switch
package main

import "fmt"

func describe(i interface{}) {
    switch v := i.(type) {
    case int:
        fmt.Println("Integer value:", v)
    case string:
        fmt.Println("String value:", v)
    case bool:
        fmt.Println("Boolean value:", v)
    default:
        fmt.Println("Unknown type")
    }
}

func main() {
    describe(42)
    describe("Hello, Go!")
    describe(true)
    describe(3.14)
}

Explanation:

  • switch v := i.(type): यह type switch है जो i की type को check करता है और उसके आधार पर specific case को execute करता है।
  • Output: "Integer value: 42", "String value: Hello, Go!", "Boolean value: true", और "Unknown type"।

Summary

  • Types in Go: Go में built-in types और custom types होते हैं, जिन्हें आप define और use कर सकते हैं।
  • Type Assertions: Type assertions का उपयोग interfaces में stored values की underlying type को retrieve करने के लिए किया जाता है।
  • Type Assertion with ok: Type assertion successful होने पर एक boolean ok return करता है, जिसे आप check कर सकते हैं।
  • Type Assertion Without Checking: बिना checking के type assertion का उपयोग किया जा सकता है, लेकिन fail होने पर यह panic करेगा।
  • Type Switch: Type switch का उपयोग multiple type assertions को handle करने के लिए किया जाता है।

Types और Type Assertions Go में flexibility और safety को manage करने के powerful tools हैं। इन्हें समझना और effectively use करना Go programming में बहुत आवश्यक है। अगर आपको types और type assertions के बारे में और जानकारी चाहिए या कोई specific सवाल है, तो बताइए!

No comments: