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

Strings और Runes

एक Go string bytes की एक row है, लेकिन एक rune एक पूरा character है (फैंसी वाले जैसे emoji भी), इसलिए Go आपको असली दुनिया के text के साथ काम करने की ज़रूरत पड़ने पर दोनों views देता है।
Syntax
go
s := "text"
r := 'x'  // rune
bytes := []byte(s)
runes := []rune(s)

Strings Are Byte Sequences

एक Go string मूल रूप से bytes का एक immutable sequence है। साधारण ASCII text के लिए हर byte एक character है, लेकिन Unicode text (जैसे emoji या non-Latin scripts) के लिए एक single character को UTF-8 उपयोग करके कई bytes में encode किया जा सकता है।

उदाहरण: Strings Are Byte Sequences

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() {
	// Short declaration: create s and infer the type
	s := "Go"
	// Print the values followed by a newline
	fmt.Println("bytes:", len(s))
	// Print the values followed by a newline
	fmt.Println("first byte:", s[0])
}

What Is a Rune?

एक rune एक Unicode code point -- एक असली, इंसान द्वारा महसूस किया जाने वाला character -- represent करता है, UTF-8 में encode होने में चाहे कितने bytes लगें। किसी string को []rune में convert करना आपको एक slice देता है जहां हर element बिल्कुल एक character है।

Note: Go में rune सिर्फ int32 का एक alias है।

उदाहरण: What Is a Rune?

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() {
	// Short declaration: create s and infer the type
	s := "café"
	// Print the values followed by a newline
	fmt.Println("byte length:", len(s))
	// Print the values followed by a newline
	fmt.Println("rune length:", len([]rune(s)))
}

Iterating Correctly with for range

किसी string पर 'for range' उपयोग करना इसे अपने आप rune by rune decode करता है, आपको हर step पर सही starting byte index और पूरा Unicode code point देते हुए, जो multi-byte characters को अलग करने के बजाय सही से handle करता है।

Note: अगर string में non-ASCII characters हो सकते हों तो कभी साधारण byte index से loop न करें।

उदाहरण: Iterating Correctly with for range

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() {
	for i, r := range "café" {
		// Print using a format string
		fmt.Printf("index %d: %c\n", i, r)
	}
}

Strings Are Immutable

बनने के बाद, Go string की contents को कभी जगह पर बदला नहीं जा सकता -- concatenation जैसे operations जो string को modify करते दिखते हैं असल में एक बिल्कुल नई string produce करते हैं। text को efficiently टुकड़ा-टुकड़ा करके बनाने के लिए, Go strings.Builder प्रदान करता है।

उदाहरण: Strings Are Immutable

markup
package main

import "fmt"

func main() {
	s := "Hello"
	s = s + ", Go!" // creates a new string, doesn't mutate the old one
	fmt.Println(s)
}
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. s[i] से किसी string को index करना और एक character की उम्मीद करना, जबकि यह असल में एक single byte return करता है, multi-byte Unicode characters पर टूटते हुए।
  2. characters गिनने के लिए len(s) उपयोग करना जब string में multi-byte UTF-8 characters हों -- len byte count देता है, character count नहीं।
  3. किसी string को जगह पर modify करने की कोशिश करना, जैसे s[0] = H, जो fail होता है क्योंकि Go में strings immutable हैं।
चैप्टर सारांश
  • Go strings bytes के immutable sequences हैं, आमतौर पर UTF-8 encoded text रखते हुए।
  • एक rune एक single Unicode code point (एक असली character) represent करता है, int32 के रूप में stored।
  • किसी string पर 'for range' rune by rune iterate करता है, multi-byte characters को सही से handle करते हुए।
  • non-ASCII strings के लिए len(s) byte count देता है, character count नहीं।
🔒

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.