php editor Banana will introduce you how to use go-yaml to marshal integer values into hexadecimal. go-yaml is a Go language library for processing YAML format data, which provides a simple and easy-to-use API. To marshal an integer value into hexadecimal, we can first convert the integer to a byte slice and then use the fmt.Sprintf function to format the byte slice into a hexadecimal string. Finally, we can use the go-yaml library to write the formatted string into a YAML file. This method is simple and efficient and can meet our needs. Next, let us take a closer look at the specific implementation steps.
I have a struct with an integer field that is meaningful to humans in a hexadecimal representation. For example, set it to the Vendor ID field.
I want to save this data to a YAML file for manual editing and then load it from the file. As far as I understand there is no problem with hexadecimal representation of numbers in YAML itself, but go-yaml
(I use v3
) encodes integers in decimal form and I haven't found a way Normal way to have it save them in hexadecimal form.
Let’s take the following code as a starting point:
import ( //... "gopkg.in/yaml.v3" ) type DeviceInfo struct { VendorId uint32 `yaml:"vendorid"` } func main() { deviceInfo := DeviceInfo{VendorId: 0xdeadbeef} yml, err := yaml.Marshal(deviceInfo) if err != nil { log.Fatal(err) } fmt.Println(string(yml)) }
This code generates YAML with decimal values:
vendorid: 3735928559
Next, go-yaml
allows you to create custom marshalers for your own types. I did this (I intentionally omitted the 0x
prefix in the fmt.Sprintf()
format string):
type Uint32Hex uint32 func (U Uint32Hex) MarshalYAML() (interface{}, error) { return fmt.Sprintf("%x", U), nil } type DeviceInfo struct { VendorId Uint32Hex `yaml:"vendorid"` } func main() { // the same code }
This code generates hexadecimal values without the 0x
prefix (which is logical for now):
vendorid: deadbeef
But if I add 0x
prefix in custom marshaler:
func (U Uint32Hex) MarshalYAML() (interface{}, error) { return fmt.Sprintf("0x%x", U), nil }
The value is generated correctly, but instead of a number, it is a string:
vendorid: "0xdeadbeef"
In order to unmarshal this row into numbers, I also had to write a custom unmarshaler. I don't like this solution.
Finally I have the following questions:
Is there an easy way to generate hexadecimal numbers using go-yaml
that I haven't found?
Is it possible to make a custom encoder based on go-yaml
as an extension of the package, without changing the package itself? For me, a more convenient way is to pass the format tag in the structure description, for example, like this:
type DeviceInfo struct { VendorId uint32 `yaml:"vendorid,hex"` }
If this requires changes to the package code, what is Go's approach to this situation? Just copy the package file into my project, modify it as needed and import locally?
The problem here is that quoting the string in yaml is optional, but go-yaml
uses the same as the JSON encoder Internal architecture. This means that custom marshaling is handled first and then the citation logic is applied completely independently. deadbeef
is not quoted, but 0xdeadbeef
is quoted because the latter is a number . It's quoted so that it doesn't accidentally unmarshal into a number because your custom marshaler returns a string when it knows it's supposed to be a string. Since deadbeef
cannot be read as a valid number, no quotes are needed. You can do two things:
func (U *Uint32Hex) UnmarshalYAML(value *yaml.Node) error { parsed, err := strconv.ParseUint(value.Value, 0, 32) *U = Uint32Hex(parsed) return err }
go-yaml
and modify it. If you do this, the imports in the source file should not be changed. Instead, you should add the replace
directive to go.mod
like this: require gopkg.in/yaml.v3 v3.0.1 replace gopkg.in/yaml.v3 v3.0.1 => ../yaml.v3 // Your local path to the fork
I prefer solution 1 because it allows you to serialize the values any way you like without deviating from the yaml generated by others, and doesn't require you to maintain a branch.
The above is the detailed content of How to marshal integer values to hex using go-yaml?. For more information, please follow other related articles on the PHP Chinese website!