https://github.com/susji/typestringer
typestringer is a program for generating Go code based on type names
https://github.com/susji/typestringer
code-generation go gogenerate gogenerator golang stringer type typename types
Last synced: 11 months ago
JSON representation
typestringer is a program for generating Go code based on type names
- Host: GitHub
- URL: https://github.com/susji/typestringer
- Owner: susji
- License: gpl-3.0
- Created: 2024-04-23T21:31:29.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-04-23T21:50:32.000Z (about 2 years ago)
- Last Synced: 2025-06-03T15:11:47.334Z (about 1 year ago)
- Topics: code-generation, go, gogenerate, gogenerator, golang, stringer, type, typename, types
- Language: Go
- Homepage:
- Size: 17.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# typestringer
This program can be used to generate customizable code based on Go type names.
One example would be the generation of `String()` functions to fulfill [the
Stringer interface](https://pkg.go.dev/fmt#Stringer).
# Usage
$ typestringer -help
Several options exist for customizing the generated code. `-include` and
`-ignore` may be used to define regular expressions for matching or not matching
specific types. Diagnostic output is written to standard error and generated
code by default to files located in the package paths. `-stdout` may be used to
redirect the generated code to standard output.
As usual, typestringer may also be executed with `go generate` by including a
comment such as the following somewhere in your code:
```go
//go:generate typestringer
```
# Installing
Builds for many platforms can be found
[here](https://github.com/susji/typestringer/releases). You may also use the Go
toolchain:
$ go install github.com/susji/typestringer@latest
# Examples
The examples below use the typestringer code.
## Basic
The type-specific format string is expanded with the type name passed two times
so escaping other formatting directives must be done with `%%`:
```shell
$ typestringer \
-stdout \
-preamble 'import fmt' \
-fmt 'func (t %s) String() string { return fmt.Sprintf("%s=%%v", t) }'
```
The generated code looks like this:
```go
// Automatically generated by typestringer with the following parameters:
// -stdout
// -preamble
// import fmt
// -fmt
// func (t %s) String() string { return fmt.Sprintf("%s=%%v", t) }
package main
import fmt
func (t stringsvar) String() string { return fmt.Sprintf("stringsvar=%v", t) }%
```
## Chaining
If specific types should be treated differently and one generated file is
desired, something like the following may be done:
```shell
$ { typestringer \
-stdout \
-preamble 'import fmt' \
-include '^FIRST$' \
-fmt 'func (t %s) String() string { return fmt.Sprintf("%s=%%v", t) }'$'\n' \
./generator/testdata/two;
typestringer \
-stdout \
-no-package \
-include '^SECOND$' \
-fmt 'func (t %s) String() string { return "%s" }'$'\n' \
./generator/testdata/two;
} 2>/dev/null
```
The generated code looks like this:
```go
// Automatically generated by typestringer with the following parameters:
// -stdout
// -preamble
// import fmt
// -include
// ^FIRST$
// -fmt
// func (t %s) String() string { return fmt.Sprintf("%s=%%v", t) }
// ./generator/testdata/two
package two
import fmt
func (t FIRST) String() string { return fmt.Sprintf("FIRST=%v", t) }
// Automatically generated by typestringer with the following parameters:
// -stdout
// -no-package
// -include
// ^SECOND$
// -fmt
// func (t %s) String() string { return "%s" }
// ./generator/testdata/two
func (t SECOND) String() string { return "SECOND" }
```