← Back to Go Course | Chapter 7: Interfaces | Lesson 5 of 7

Stringer Interface क्या है

Stringer interface implement करना आपके custom type को यह सिखाने जैसा है कि जब कोई इसे print करे तो politely कैसे introduce हो, एक confusing default दिखाने के बजाय।
Syntax
go
func (t Type) String() string {
    return "text"
}

Implementing Stringer

standard library का fmt.Stringer interface बिल्कुल एक method मांगता है, String() string। इसे implement करने वाली कोई भी type इसके Println, Printf के %v, या similar से print होने पर अपने आप उपयोग होती है।

उदाहरण: Implementing Stringer

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

// Import the fmt package
import "fmt"

// Define a struct type named Point
type Point struct {
	X, Y int
}

// Define the method String on type Point
func (p Point) String() string {
	// Sprintf builds a formatted string instead of printing it
	return fmt.Sprintf("(%d, %d)", p.X, p.Y)
}

// main is where the program starts running
func main() {
	// Short declaration: create p and infer the type
	p := Point{3, 4}
	// Print the values followed by a newline
	fmt.Println(p)
}

Without vs With Stringer

बिना String() method वाला struct अपना default, थोड़ा verbose field-by-field representation print करता है, जबकि Stringer implement करना आपको पूरा control देता है कि print होने पर यह बिल्कुल कैसा पढ़ा जाए।

उदाहरण: Without vs With Stringer

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

// Import the fmt package
import "fmt"

type Status int

const (
	Active Status = iota
	Inactive
)

// Define the method String on type Status
func (s Status) String() string {
	// Check whether s == Active
	if s == Active {
		// Send "Active" back to the caller
		return "Active"
	}
	// Send "Inactive" back to the caller
	return "Inactive"
}

// main is where the program starts running
func main() {
	// Print the values followed by a newline
	fmt.Println(Active)
	fmt.Println(Inactive)
}

Avoiding Infinite Recursion

एक आम गलती अपने ही String() method के अंदर receiver p पर fmt.Sprintf("%v", p) call करना है -- चूंकि %v String() को फिर से trigger करता है, यह हमेशा के लिए recurse करता है। individual fields से string बनाना इस समस्या से बचाता है।

Note: हमेशा String() output को struct की individual fields से बनाएं, कभी receiver को पूरी तरह से नहीं।

उदाहरण: Avoiding Infinite Recursion

markup
package main

import "fmt"

type Temp struct {
	Celsius float64
}

func (t Temp) String() string {
	return fmt.Sprintf("%.1f°C", t.Celsius) // uses the field, not t itself
}

func main() {
	fmt.Println(Temp{Celsius: 21.5})
}
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. method को बिल्कुल 'String() string' के अलावा कुछ और नाम देना -- fmt सिर्फ वह exact signature Stringer interface के लिए पहचानता है।
  2. अपने ही String() method के अंदर receiver पर fmt.Sprintf("%v", t) call करके गलती से infinite recursion का कारण बनना।
  3. यह भूल जाना कि pointer receiver से defined एक String() method सिर्फ तब लागू होता है जब type के pointer को print किया जाए, value को खुद नहीं।
चैप्टर सारांश
  • fmt.Stringer interface को एक method चाहिए: String() string।
  • इसे implement करना fmt.Println और %v को अपने आप आपका custom, readable representation उपयोग करने देता है।
  • String() के अंदर receiver पर खुद fmt formatting functions call करना infinite recursion का कारण बनता है -- इसके बजाय underlying fields को format करें।
  • Stringer रोज़मर्रा के उपयोग में Go के छोटे, implicit interfaces का एक शानदार उदाहरण है।
🔒

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.