Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
529 views
in Technique[技术] by (71.8m points)

go - How do i print Variable name instead of the value assigned to the variable?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

When you pass a value to a function, the variable name is not passed to it, so it is impossible to print the name of the variable. It's not even possible to get the names of the parameters, for details, see Getting method parameter names.

The closest you can do is use a struct as the parameter type, and you can get and print the name of the fields (using reflection). Or use a map, and pass the variable name as the key.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...