Stringer Interface क्या है
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
// 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)
}
Login to try C/C++/Java/PHP code in the editor
Without vs With Stringer
बिना String() method वाला struct अपना default, थोड़ा verbose field-by-field representation print करता है, जबकि Stringer implement करना आपको पूरा control देता है कि print होने पर यह बिल्कुल कैसा पढ़ा जाए।
उदाहरण: Without vs With Stringer
// 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)
}
Login to try C/C++/Java/PHP code in the editor
Avoiding Infinite Recursion
एक आम गलती अपने ही String() method के अंदर receiver p पर fmt.Sprintf("%v", p) call करना है -- चूंकि %v String() को फिर से trigger करता है, यह हमेशा के लिए recurse करता है। individual fields से string बनाना इस समस्या से बचाता है।
उदाहरण: Avoiding Infinite Recursion
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})
}
Login to try C/C++/Java/PHP code in the editor
- method को बिल्कुल 'String() string' के अलावा कुछ और नाम देना -- fmt सिर्फ वह exact signature Stringer interface के लिए पहचानता है।
- अपने ही String() method के अंदर receiver पर fmt.Sprintf("%v", t) call करके गलती से infinite recursion का कारण बनना।
- यह भूल जाना कि 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: