Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

🕵️‍♂️ Extract a string representation of Go type

License

NotificationsYou must be signed in to change notification settings

samber/go-type-to-string

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

tagGo VersionGoDocBuild StatusGo reportCoverageContributorsLicense

Motivations

For thesamber/do project, I needed to convert a Go type into a string. I used to convert it withfmt.Sprintf("%T", t) ->mypkg.MyStruct, but it does not insert package path into type representation, leading to collision when types from different pacakges match.

This package export type using the following representation:

*[]**<-chan   github.com/samber/example    .Example||^||Typename|^|Thepackagepath (includingpackagename)^Typeindicators (map,slice,pointer,channel...)

This library supports:

  • primitive types
  • pointers
  • structs
  • functions with input and output
  • vaargs
  • interfaces
  • maps
  • arrays
  • slices
  • channels
  • generics
  • anonymous types
  • named types

Known limitations:

  • structs in anonymous types
  • structs in generic type
  • any("foobar") is currently reported asany instead ofstring (see#2)

Examples

Using the following types:

package exampletypetestStructstruct{}typetestGeneric[Tany]struct{tT }typetestNamedTypetestStruct
TypeExported
intint
*int*int
**[]*int**[]*int
**[]*map[int]bool**[]*map[int]bool
func (a string, b bool) intfunc (string, bool) int
func(int, ...string) (bool, error)func(int, ...string) (bool, error)
testStructgithub.com/samber/example.testStruct
*testStruct*github.com/samber/example.testStruct
***testStruct***github.com/samber/example.testStruct
***testNamedType***github.com/samber/example.testNamedType
[][3]***testStruct[][3]***github.com/samber/example.testStruct
testGeneric[string]github.com/samber/example.testGeneric[string]
*map[testStruct]chan<- testGeneric[string]*map[github.com/samber/example.testStruct]chan<- github.com/samber/example.testGeneric[string]

See more exampleshere

🚀 Install

go get github.com/samber/go-type-to-string

This library is v1 and follows SemVer strictly. No breaking changes will be made to exported APIs before v2.0.0.

💡 How to

GoDoc:https://pkg.go.dev/github.com/samber/go-type-to-string

package exampleimport converter"github.com/samber/go-type-to-string"typeExamplestruct{foostringbarint}funcmain() {name1:=converter.GetType[*Example]()// "*github.com/samber/example.Example"name2:=converter.GetValueType(Example{})// "github.com/samber/example.Example"}

🤝 Contributing

Don't hesitate ;)

# Install some dev dependenciesmake tools# Run testsmaketest# ormake watch-test

👤 Contributors

Contributors

💫 Show your support

Give a ⭐️ if this project helped you!

GitHub Sponsors

📝 License

Copyright © 2023Samuel Berthe.

This project isMIT licensed.


[8]ページ先頭

©2009-2025 Movatter.jp