Type का Conversion
In this page:
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
// 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)
}
Login to try C/C++/Java/PHP code in the editor
Truncation with Narrowing Conversions
किसी बड़े numeric type से छोटे में convert करना (जैसे int से int8) अगर value fit न हो तो कोई error नहीं देता -- यह चुपचाप truncate या wrap around हो जाता है, जो source value range से बाहर होने पर सूक्ष्म bugs introduce कर सकता है।
उदाहरण: Truncation with Narrowing Conversions
// 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)
}
Login to try C/C++/Java/PHP code in the editor
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 करते हैं।
उदाहरण: String to Number with strconv
// 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)
}
Login to try C/C++/Java/PHP code in the editor
Number to String
दूसरी तरफ, किसी number को इसके textual representation में convert करना भी strconv उपयोग करता है, आमतौर पर ints के लिए strconv.Itoa, बजाय एक साधारण string(n) conversion के, जो भ्रामक रूप से number को इसके बजाय एक single Unicode character में convert कर देगा।
उदाहरण: Number to String
// 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 + "\"")
}
Login to try C/C++/Java/PHP code in the editor
- mixed arithmetic में Go से अपने आप एक int को float64 में convert करने की उम्मीद करना, जबकि इसे असल में एक explicit conversion चाहिए।
- किसी बड़े int64 को int8 में convert करना और error के बजाय चुपचाप wrapped/truncated values से हैरान होना।
- 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: