Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FmtString ¶
func FmtString(stg Formattable, f fmt.State, c rune)
FmtString implements the fmt.Formatter interface for Formattable types. It handles various format verbs to provide consistent string formatting across different output contexts.
Supported format verbs:
- %v: Uses String() representation (default format)
- %+v: Uses String() representation (verbose format)
- %#v: Uses GoString() representation (Go-syntax format)
- %s: Uses String() representation (string format)
- %q: Uses quoted String() representation (quoted string format)
- default: Uses String() representation for any other verb
The function delegates to the appropriate method of the Formattable interface based on the format verb and flags provided by fmt.State.
Parameters:
- stg: The Formattable value to format
- f: The fmt.State that provides formatting context and flags
- c: The format verb (rune) being used
Example usage:
type MyType struct {
value int
}
func (m MyType) Format(f fmt.State, verb rune) {
formatting.FmtString(m, f, verb)
}
func (m MyType) String() string {
return fmt.Sprintf("MyType(%d)", m.value)
}
func (m MyType) GoString() string {
return fmt.Sprintf("MyType{value: %d}", m.value)
}
// Usage:
mt := MyType{value: 42}
fmt.Printf("%v\n", mt) // Output: MyType(42)
fmt.Printf("%#v\n", mt) // Output: MyType{value: 42}
fmt.Printf("%s\n", mt) // Output: MyType(42)
fmt.Printf("%q\n", mt) // Output: "MyType(42)"
func TypeInfo ¶
TypeInfo returns a string representation of the type of the given value. It uses reflection to determine the type and removes the leading asterisk (*) from pointer types to provide a cleaner type name.
This function is useful for generating human-readable type information in string representations, particularly for generic types where the concrete type needs to be displayed.
Parameters:
- v: The value whose type information should be extracted
Returns:
- A string representing the type name, with pointer prefix removed
Example usage:
// For non-pointer types
TypeInfo(42) // Returns: "int"
TypeInfo("hello") // Returns: "string"
TypeInfo([]int{1, 2, 3}) // Returns: "[]int"
// For pointer types (asterisk is removed)
var ptr *int
TypeInfo(ptr) // Returns: "int" (not "*int")
// For custom types
type MyStruct struct{ Name string }
TypeInfo(MyStruct{}) // Returns: "formatting.MyStruct"
TypeInfo(&MyStruct{}) // Returns: "formatting.MyStruct" (not "*formatting.MyStruct")
// For interface types
var err error = fmt.Errorf("test")
TypeInfo(err) // Returns: "errors.errorString"
Types ¶
type Formattable ¶
Formattable is a composite interface that combines multiple formatting capabilities from the Go standard library. Types implementing this interface can be formatted in various contexts including string conversion, custom formatting, Go syntax representation, and structured logging.
This interface is particularly useful for types that need to provide consistent formatting across different output contexts, such as logging, debugging, and user-facing displays.
Embedded Interfaces:
- fmt.Stringer: Provides String() string method for basic string representation
- fmt.Formatter: Provides Format(f fmt.State, verb rune) for custom formatting with verbs like %v, %s, %+v, etc.
- fmt.GoStringer: Provides GoString() string method for Go-syntax representation (used with %#v)
- slog.LogValuer: Provides LogValue() slog.Value for structured logging with the slog package
Example Implementation:
type User struct {
ID int
Name string
}
// String provides a simple string representation
func (u User) String() string {
return fmt.Sprintf("User(%s)", u.Name)
}
// Format provides custom formatting based on the verb
func (u User) Format(f fmt.State, verb rune) {
switch verb {
case 'v':
if f.Flag('+') {
fmt.Fprintf(f, "User{ID: %d, Name: %s}", u.ID, u.Name)
} else {
fmt.Fprint(f, u.String())
}
case 's':
fmt.Fprint(f, u.String())
}
}
// GoString provides Go-syntax representation
func (u User) GoString() string {
return fmt.Sprintf("User{ID: %d, Name: %q}", u.ID, u.Name)
}
// LogValue provides structured logging representation
func (u User) LogValue() slog.Value {
return slog.GroupValue(
slog.Int("id", u.ID),
slog.String("name", u.Name),
)
}
Usage:
user := User{ID: 1, Name: "Alice"}
fmt.Println(user) // Output: User(Alice)
fmt.Printf("%+v\n", user) // Output: User{ID: 1, Name: Alice}
fmt.Printf("%#v\n", user) // Output: User{ID: 1, Name: "Alice"}
slog.Info("user", "user", user) // Structured log with id and name fields