user 13145131541
user 13145131541

Reputation: 23

Printing all values of a struct with mixed values?

Is there any way of printing a struct with mixed value types, including pointer types, such that all value are shown? For example:

package main

import (
    "fmt"
)

type test struct {
  Str string
  Ptr *string
}

func main() {
  s := "some string"
  p := &s

  t := test{
     Str: s,
     Ptr: p,
  }

  fmt.Printf("%#v\n", t)
}

I want something like: main.test{Str:"some string", Ptr:(*string)("some string"}
instead of: main.test{Str:"some string", Ptr:(*string)(0x1040a120)}

https://play.golang.org/p/YkZrPOeQ_Y

Upvotes: 2

Views: 92

Answers (1)

Ammar Bandukwala
Ammar Bandukwala

Reputation: 1572

There's no fmt verb you could use for that functionality. You could implement Stringer on your struct and have full control over how the struct is printed.

Upvotes: 1

Related Questions