Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/richjyoung/wtf
wtf.IsThis Golang Package
https://github.com/richjyoung/wtf
Last synced: 8 days ago
JSON representation
wtf.IsThis Golang Package
- Host: GitHub
- URL: https://github.com/richjyoung/wtf
- Owner: richjyoung
- License: mit
- Created: 2022-11-22T00:00:56.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2022-11-22T09:25:35.000Z (almost 2 years ago)
- Last Synced: 2024-06-20T12:52:49.057Z (5 months ago)
- Language: Go
- Size: 8.79 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# wtf.IsThis
Golang is great until at runtime you are dealing with an interface for which the underlying implementation is buried in a package somewhere.
Yes it's easier than other languages to click through and find it, or...
```golang
wtf.IsThis(1) // int
wtf.IsThis("Hello, World!") // string
wtf.IsThis(AStruct{}) // pkg.AStruct
wtf.IsThis(&AStruct{}) // *pkg.AStruct
wtf.IsThis([]*AStruct{}) // []*pkg.AStruct
wtf.IsThis(map[int]*AStruct{}) // map[int]*pkg.AStruct
wtf.IsThis(make(<-chan int)) // <-chan int
wtf.IsThis(func(int) error { return nil }) // func (int) error {}// In cases where the type is nil or contains an empty interface, wtf.NoIdea is returned.
wtf.IsThis(nil) // ¯\_(ツ)_/¯
wtf.IsThis(map[string]interface{}{}) // map[string]¯\_(ツ)_/¯// Provides more detail for errors which may implement Unwrap
e1 := fmt.Errorf("error 1")
e2 := fmt.Errorf("error 2 - %w", e1)
e3 := fmt.Errorf("error 3 - %w", e2)wtf.IsThisError(e3) // Returns:
// *fmt.wrapError[error 3 - error 2 - error 1]
// └─*fmt.wrapError[error 2 - error 1]
// └─*errors.errorString[error 1]
```More examples in the [test](./wtf_test.go).