strconv Package का उपयोग
n, err := strconv.Atoi(s)
s := strconv.Itoa(n)
f, err := strconv.ParseFloat(s, 64)
b, err := strconv.ParseBool(s)
String to Int and Back
strconv.Atoi किसी string को base-10 integer के रूप में parse करता है, अगर invalid हो तो एक error return करते हुए, और strconv.Itoa उल्टा करता है, एक int को इसके string form में convert करते हुए।
उदाहरण: String to Int and Back
// 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("123")
// Handle the error if one occurred
if err != nil {
// Print the values followed by a newline
fmt.Println("parse error:", err)
return
}
// Print the values followed by a newline
fmt.Println(n + 1)
fmt.Println(strconv.Itoa(n + 1))
}
Login to try C/C++/Java/PHP code in the editor
Parsing Floating-Point Numbers
strconv.ParseFloat किसी string को एक float64 में convert करता है, parsing के दौरान precision उम्मीदें control करने के लिए एक bit-size argument (float64 के लिए 64) लेते हुए।
उदाहरण: Parsing Floating-Point Numbers
// 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 f, err and infer the type
f, err := strconv.ParseFloat("3.14", 64)
// Handle the error if one occurred
if err != nil {
// Print the values followed by a newline
fmt.Println("parse error:", err)
return
}
// Print the values followed by a newline
fmt.Println(f * 2)
}
Login to try C/C++/Java/PHP code in the editor
Parsing Booleans
strconv.ParseBool true और false के कई आम textual representations पहचानता है (जैसे "1"/"0", "t"/"f", "true"/"false"), उनमें से किसी को भी एक proper bool value में convert करते हुए।
उदाहरण: Parsing Booleans
// 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 b, err and infer the type
b, err := strconv.ParseBool("true")
// Handle the error if one occurred
if err != nil {
// Print the values followed by a newline
fmt.Println("parse error:", err)
return
}
// Print the values followed by a newline
fmt.Println("parsed bool:", b)
}
Login to try C/C++/Java/PHP code in the editor
- strconv.Atoi/ParseFloat से returned error को नज़रअंदाज़ करना, यह मानते हुए कि input string हमेशा valid है।
- किसी float पर strconv.Itoa उपयोग करना, जबकि Itoa सिर्फ int accept करता है -- floats को इसके बजाय strconv.FormatFloat चाहिए।
- यह भूल जाना कि strconv.ParseBool कई textual forms ("1", "t", "true", आदि) accept करता है, सिर्फ literal words true/false नहीं।
- strconv.Atoi और strconv.Itoa string और int के बीच convert करते हैं।
- strconv.ParseFloat और strconv.FormatFloat floating-point numbers handle करते हैं, एक bit-size argument मांगते हुए।
- strconv.ParseBool "true"/"1" जैसी strings को एक bool में convert करता है।
- हर strconv parsing function result के साथ एक error return करता है, जिसे हमेशा जांचना चाहिए।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: