← Back to Go Course | Chapter 14: Standard Library & HTTP | Lesson 2 of 10

strconv Package का उपयोग

strconv numbers और numbers के text versions के बीच Go का translator है, सावधानी से आगे-पीछे convert करते हुए ताकि कुछ भी खो या गलत न पढ़ा जाए।
Syntax
go
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

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("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))
}

Parsing Floating-Point Numbers

strconv.ParseFloat किसी string को एक float64 में convert करता है, parsing के दौरान precision उम्मीदें control करने के लिए एक bit-size argument (float64 के लिए 64) लेते हुए।

उदाहरण: Parsing Floating-Point Numbers

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 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)
}

Parsing Booleans

strconv.ParseBool true और false के कई आम textual representations पहचानता है (जैसे "1"/"0", "t"/"f", "true"/"false"), उनमें से किसी को भी एक proper bool value में convert करते हुए।

उदाहरण: Parsing Booleans

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 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)
}
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. strconv.Atoi/ParseFloat से returned error को नज़रअंदाज़ करना, यह मानते हुए कि input string हमेशा valid है।
  2. किसी float पर strconv.Itoa उपयोग करना, जबकि Itoa सिर्फ int accept करता है -- floats को इसके बजाय strconv.FormatFloat चाहिए।
  3. यह भूल जाना कि 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 करता है, जिसे हमेशा जांचना चाहिए।

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.