← Back to Go Course | Chapter 2: Variables & Types | Lesson 6 of 7

Type का Conversion

Type conversion एक shaped cup से दूसरे में juice डालने जैसा है -- juice वही है, लेकिन आपको इसे जान-बूझकर डालना है, क्योंकि Go कभी आपके लिए यह अपने आप नहीं करता।
Syntax
go
targetType(value)

strconv.Itoa(intValue)
strconv.Atoi(stringValue)

Converting Between Numeric Types

Go numeric types के बीच कभी implicitly convert नहीं करता, यहां तक कि int और float64 जैसी compatible लगने वाली types के बीच भी -- आपको TargetType(value) syntax उपयोग करके explicitly convert करना चाहिए। यह सख्ती उन bugs को पकड़ती है जहां precision चुपचाप खो सकती थी।

उदाहरण: Converting Between Numeric Types

markup
// Every Go file belongs to a package; main builds an executable
package main

// Import the fmt package
import "fmt"

// main is where the program starts running
func main() {
	// Declare int and set it to 10
	var i int = 10
	// Declare float64 and set it to float64(i) / 3
	var f float64 = float64(i) / 3
	// Print the values followed by a newline
	fmt.Println("as float:", f)
}

Truncation with Narrowing Conversions

किसी बड़े numeric type से छोटे में convert करना (जैसे int से int8) अगर value fit न हो तो कोई error नहीं देता -- यह चुपचाप truncate या wrap around हो जाता है, जो source value range से बाहर होने पर सूक्ष्म bugs introduce कर सकता है।

Note: किसी छोटे type में convert करने से पहले हमेशा जांचें कि value fit होती है या नहीं, चुपचाप data loss से बचने के लिए।

उदाहरण: Truncation with Narrowing Conversions

markup
// Every Go file belongs to a package; main builds an executable
package main

// Import the fmt package
import "fmt"

// main is where the program starts running
func main() {
	// Declare int and set it to 300
	var big int = 300
	// Declare int8 and set it to int8(big)
	var small int8 = int8(big)
	// Print the values followed by a newline
	fmt.Println("300 truncated to int8:", small)
}

String to Number with strconv

"42" जैसी किसी string को एक असली number में convert करना Go के मायने में एक type conversion नहीं है -- इसके लिए strconv package के parsing functions चाहिए, जैसे strconv.Atoi, जो fail हो सकते हैं (जैसे non-numeric input पर) और इसलिए result के साथ एक error भी return करते हैं।

Note: parsed value उपयोग करने से पहले हमेशा strconv functions से return हुई error जांचें।

उदाहरण: String to Number with strconv

markup
// Every Go file belongs to a package; main builds an executable
package main

// Import the packages this program uses
import (
	"fmt"
	"strconv"
)

// main is where the program starts running
func main() {
	// Short declaration: create n, err and infer the type
	n, err := strconv.Atoi("42")
	// Handle the error if one occurred
	if err != nil {
		// Print the values followed by a newline
		fmt.Println("conversion failed:", err)
		return
	}
	// Print the values followed by a newline
	fmt.Println("parsed number:", n+8)
}

Number to String

दूसरी तरफ, किसी number को इसके textual representation में convert करना भी strconv उपयोग करता है, आमतौर पर ints के लिए strconv.Itoa, बजाय एक साधारण string(n) conversion के, जो भ्रामक रूप से number को इसके बजाय एक single Unicode character में convert कर देगा।

उदाहरण: Number to String

markup
// Every Go file belongs to a package; main builds an executable
package main

// Import the packages this program uses
import (
	"fmt"
	"strconv"
)

// main is where the program starts running
func main() {
	// Short declaration: create score and infer the type
	score := 95
	// Short declaration: create text and infer the type
	text := strconv.Itoa(score)
	// Print the values followed by a newline
	fmt.Println("score as text: \"" + text + "\"")
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. mixed arithmetic में Go से अपने आप एक int को float64 में convert करने की उम्मीद करना, जबकि इसे असल में एक explicit conversion चाहिए।
  2. किसी बड़े int64 को int8 में convert करना और error के बजाय चुपचाप wrapped/truncated values से हैरान होना।
  3. strconv functions को गलत तरीके से उपयोग करना, जैसे strconv.Atoi को एक non-numeric string पास करना और returned error को नज़रअंदाज़ करना।
चैप्टर सारांश
  • Go को अलग-अलग numeric types के बीच explicit conversion चाहिए, यहां तक कि int और float64 के बीच भी।
  • Conversion syntax TargetType(value) है, जैसे float64(x) या int(y)।
  • string और numeric types के बीच convert करने के लिए strconv package चाहिए, साधारण type conversion नहीं।
  • Narrowing conversions (जैसे int64 से int8) चुपचाप data truncate कर सकते हैं, इसलिए इन्हें सावधानी से उपयोग करें।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.