For my application, it does not matter if the string is human readable or not.
Short answer: keep in mind that strings are immutable types. Create your struct normally, and realize that the struct only contains a reference to the string, not a magically resizing segment of the struct that expands to include your volatile string.
if your intention is to get value out of struct and store it as a primitive value then try like below, create external table table2 ( a string, b_c string, b_d string, c string ) insert overwrite table table2 select a,b.c,b.e,c from table1; Let me know if this helps.
What is an idiomatic way to customize the representation of a custom struct in a formatted string? There is no customizing the string representation of a type. Build it in pieces each time by calling individual fields. Create a wrapper function that accepts your type and outputs a string.
If it's a "one way" serialization (for debugging or logging or whatever) then fmt.Printf("%#v", var)
is very nice. (Update: to put the output into a string instead of printing it, use str := fmt.Sprintf("%#v", var)
.
If size matters you can use %v
, but I like %#v
because it will also include the field names and the name of the struct type.
A third variation is %+v
which will include the field names, but not the struct type.
They are all documented at the top of the fmt documentation.
If you need two-way serialization JSON, Gob or XML are the easiest/built-in options in Go, see the encoding packages.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With