Translate

Type Casting in Golang

What is Type Casting in Go?

Go में Type Casting का उपयोग एक data type को दूसरे data type में convert करने के लिए किया जाता है। Go एक statically typed language है, जिसका मतलब है कि आपको variables को एक type से दूसरे type में convert करने के लिए explicit conversion की आवश्यकता होती है। Go में implicit type conversion (automatic type conversion) नहीं होती, जैसा कि कुछ अन्य programming languages में होता है।

Basic Syntax for Type Casting

Go में Type Casting की basic syntax कुछ इस प्रकार होती है:

newType(variableName)

इस syntax में newType वह type है जिसमें आप variable को convert करना चाहते हैं, और variableName वह variable है जिसे आप convert करना चाहते हैं।

1. Integer to Float Conversion

Go में आप integer types को floating-point types में convert कर सकते हैं।

Example: Integer to Float Conversion

package main

import "fmt"

func main() {
    var intVar int = 42
    var floatVar float64

    // Type casting from int to float64
    floatVar = float64(intVar)

    fmt.Println("Integer value:", intVar)
    fmt.Println("Float value:", floatVar)
}

Explanation:

  • float64(intVar): यहां integer intVar को float64 में convert किया गया है।
  • Output में integer 42 को float 42.000000 के रूप में दिखाया जाएगा।

2. Float to Integer Conversion

आप floating-point values को integer types में convert कर सकते हैं, लेकिन ध्यान दें कि इस conversion में fractional part truncate हो जाता है।

Example: Float to Integer Conversion

package main

import "fmt"

func main() {
    var floatVar float64 = 42.8
    var intVar int

    // Type casting from float64 to int
    intVar = int(floatVar)

    fmt.Println("Float value:", floatVar)
    fmt.Println("Integer value after conversion:", intVar)
}

Explanation:

  • int(floatVar): यहां floatVar को int में convert किया गया है। Conversion के बाद fractional part (.8) truncate हो जाता है, और output में integer value 42 दिखाई जाएगी।

3. Integer to String Conversion

Go में integers को string में convert करने के लिए आपको strconv package का उपयोग करना पड़ता है। Direct type casting के जरिए आप integers को strings में convert नहीं कर सकते।

Example: Integer to String Conversion

package main

import (
    "fmt"
    "strconv"
)

func main() {
    var intVar int = 123

    // Convert integer to string
    strVar := strconv.Itoa(intVar)

    fmt.Println("Integer value:", intVar)
    fmt.Println("String value:", strVar)
}

Explanation:

  • strconv.Itoa(intVar): यह function integer intVar को string में convert करता है। Itoa का मतलब है "Integer to ASCII"।
  • Output में integer 123 को string "123" के रूप में दिखाया जाएगा।

4. String to Integer Conversion

Strings को integers में convert करने के लिए भी strconv package का उपयोग किया जाता है। यह conversion तब useful होता है जब आपको user input को integer में convert करने की आवश्यकता हो।

Example: String to Integer Conversion

package main

import (
    "fmt"
    "strconv"
)

func main() {
    var strVar string = "123"
    
    // Convert string to integer
    intVar, err := strconv.Atoi(strVar)

    if err != nil {
        fmt.Println("Conversion error:", err)
    } else {
        fmt.Println("String value:", strVar)
        fmt.Println("Integer value:", intVar)
    }
}

Explanation:

  • strconv.Atoi(strVar): यह function string strVar को integer में convert करता है। Atoi का मतलब है "ASCII to Integer"।
  • Conversion के बाद, "123" string integer 123 में convert हो जाता है।

5. Type Casting Between Different Integer Types

Go में अलग-अलग sizes के integer types होते हैं (जैसे int8, int16, int32, int64), और इन types के बीच conversion किया जा सकता है।

Example: Type Casting Between Integer Types

package main

import "fmt"

func main() {
    var smallInt int8 = 127
    var bigInt int64

    // Type casting from int8 to int64
    bigInt = int64(smallInt)

    fmt.Println("Small integer value:", smallInt)
    fmt.Println("Big integer value after conversion:", bigInt)
}

Explanation:

  • int64(smallInt): यहां smallInt को int64 type में convert किया गया है।
  • Output में small integer 127 को large integer 127 के रूप में दिखाया जाएगा।

6. Byte and Rune Conversions

Go में byte एक alias है uint8 का और rune एक alias है int32 का। इन types का उपयोग characters और strings को handle करने के लिए किया जाता है।

Example: Converting String to Byte Slice

package main

import "fmt"

func main() {
    var strVar string = "Hello"
    
    // Convert string to byte slice
    byteSlice := []byte(strVar)

    fmt.Println("String value:", strVar)
    fmt.Println("Byte slice:", byteSlice)
}

Explanation:

  • []byte(strVar): यह conversion string strVar को byte slice में convert करता है।
  • Output में "Hello" string को [72 101 108 108 111] के रूप में दिखाया जाएगा, जो ASCII values हैं।

Example: Converting String to Rune Slice

package main

import "fmt"

func main() {
    var strVar string = "Hello"

    // Convert string to rune slice
    runeSlice := []rune(strVar)

    fmt.Println("String value:", strVar)
    fmt.Println("Rune slice:", runeSlice)
}

Explanation:

  • []rune(strVar): यह conversion string strVar को rune slice में convert करता है।
  • Output में "Hello" string को [72 101 108 108 111] के रूप में दिखाया जाएगा, जो Unicode values हैं।

7. Complex Number Conversions

Go में आप complex numbers को भी convert कर सकते हैं, जिसमें real और imaginary parts शामिल होते हैं।

Example: Converting Complex Numbers

package main

import "fmt"

func main() {
    var complexVar complex128 = complex(5, 7)
    var floatVar float64

    // Type casting from complex128 to float64 (only real part)
    floatVar = float64(real(complexVar))

    fmt.Println("Complex number:", complexVar)
    fmt.Println("Real part as float:", floatVar)
}

Explanation:

  • real(complexVar): यह function complex number के real part को extract करता है।
  • float64(real(complexVar)): यह conversion real part को float में convert करता है।

Summary

  • Explicit Type Casting: Go में आपको data types को convert करने के लिए explicit type casting करनी पड़ती है।
  • Integer to Float और Float to Integer: Integers को floats में और floats को integers में convert किया जा सकता है।
  • String Conversion: Strings को integers या bytes/runes में convert करने के लिए strconv package का उपयोग किया जाता है।
  • Complex Number Conversion: Complex numbers के real और imaginary parts को अलग-अलग convert किया जा सकता है।
  • Integer Type Conversion: Go के अलग-अलग integer types के बीच conversion किया जा सकता है।

Go में type conversion करते समय data loss या overflow का ध्यान रखना जरूरी है, खासकर जब आप floats को integers में convert कर रहे हों या छोटे से बड़े types में conversion कर रहे हों। अगर आपको type casting के बारे में और जानकारी चाहिए या कोई specific सवाल है, तो बताइए!

No comments: