https://github.com/jcocozza/go_struct_stringify
Convert go structs into their corresponding code as a string
https://github.com/jcocozza/go_struct_stringify
golang stringify struct
Last synced: 9 months ago
JSON representation
Convert go structs into their corresponding code as a string
- Host: GitHub
- URL: https://github.com/jcocozza/go_struct_stringify
- Owner: jcocozza
- Created: 2024-02-15T18:47:26.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-08-08T11:05:06.000Z (almost 2 years ago)
- Last Synced: 2025-03-27T15:26:28.888Z (about 1 year ago)
- Topics: golang, stringify, struct
- Language: Go
- Homepage:
- Size: 7.81 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Go Struct Stringify
This package will take a struct instance and convert it into a string.
The goal is to provide a very easy way to take a struct that exists as an instance and create the code that corresponds to that struct.
```
type myStruct struct {
A string
B int
}
myStruct := &{A: "a", B: 1}
myStructString := StructStringify(myStruct)
fmt.Print(myStructString)
--
&myStruct{A: "a", B: 1}
```
## Motivation
You're writing some integration tests for your code. You trust things to work-- these tests are more of a future proofing endeavor.
Topping it off, you have to load in some complex objects from the database. Structs with sub-structs, lists of structs, custom types etc. It's going to manually create these objects to verify things are working properly.
Here's were this package comes in, just use the `StructStringify()` function to turn a struct instance (that you just loaded in from your database, for example) into a string. Then all you need to do is print that string, and it will be ready for you to copy and paste where ever you please.