Print fields of a struct %+v

How do I print the name of the fields (here t and p) of a struct?

package main

import "fmt"

type drive interface {
	carry() string
}

type vehicle struct{
	passengers, cargo bool
}

func (t vehicle) carry() string{
	if t.cargo && t.passengers {
		return "Yes it is truck"
	}
	return "No it is not a truck"
}
// How to print the "t" and "p"?
func categorized_result(d drive){
	fmt.Println("%v\n", d)
	fmt.Println(d.carry())
}

func main(){
	fmt.Println("Hello World")
	fmt.Println("Main function starts here!")
	t := vehicle {cargo: true, passengers: true}
	p := vehicle {cargo: false, passengers: true}
	categorized_result(t)
	categorized_result(p)
}
/*
Need Output Like this:
----------------------
Hello World
Main function starts here!
T:  Yes it is truck
P:  No it is not a truck
*/

“t” and “p” are not fields of any struct in your example, and not the same as “T” or “P” that you want printed.

Why not just print what you need printed?

1 Like

Passed the variable name:
https://play.golang.org/p/KakDr--iXX2