Golang fmt.Stringer type example
package fmt
Stringer is implemented by any value that has a String method, which defines the “native” format for that value. The String method is used to print values passed as an operand to any format that accepts a string or to an unformatted printer such as Print.
Golang fmt.Stringer type usage example
func toString(a interface{}) (string, bool) {
aString, isString := a.(string)
if isString {
return aString, true
}
aBytes, isBytes := a.([]byte)
if isBytes {
return string(aBytes), true
}
aStringer, isStringer := a.(fmt.Stringer) // <-- here
if isStringer {
return aStringer.String(), true // <-- and here
}
return "", false
}
Reference :
Advertisement
Something interesting
Tutorials
+22.5k Golang : Convert Unix timestamp to UTC timestamp
+6.3k WARNING: UNPROTECTED PRIVATE KEY FILE! error message
+12.8k Swift : Convert (cast) Int or int32 value to CGFloat
+6.1k Golang : Get missing location after unmarshal binary and gob decode time.
+19.7k Golang : Archive directory with tar and gzip
+5.9k Facebook : How to force facebook to scrape latest URL link data?
+29.3k Golang : Save map/struct to JSON or XML file
+14.4k Golang : Find network of an IP address
+5.4k Golang : What is StructTag and how to get StructTag's value?
+9.4k Facebook : Getting the friends list with PHP return JSON format
+28.6k Get file path of temporary file in Go