How Do You Troubleshoot Golang Using Hexademical Values?

Problem scenario
Your Golang program has variables with values that look like this:

{0xc4200457e8 [97 49]}

You think the that underlying variables should have regular alphabetic string values. What should you do?

Solution
Use the .string() function.

When you use strings.Builder for a variable, the value will be a hexadecimal value. To retrieve the string value that you intend, you must append ".string()" to the variable.

Here is a snippet of code (without the import section that you would need) appending integers to the letter "a":

var prevar strings.Builder

for i := 0; i < 10; i++ {
  j := strconv.Itoa(i)
  if i == 0 {prevar.WriteString("a")
  } else {
  prevar.WriteString(j)
  fmt.Println(prevar.String())
  }
}

Leave a comment

Your email address will not be published. Required fields are marked *