GoLang打印结构体中变量的值

huangapple go评论90阅读模式
英文:

GoLang print value of variables in structure

问题

我不是GoLang的专家,但我需要在一个项目上进行故障排除。
现在我尝试打印相同变量的值,但输出的是地址而不是值:

attrs := core.Attributes{}
err = rtrV1.SetAttributesStruct(&attrs, sessAttrs, session.NasIP, ipv4Session, ipv6Session)
if err1, ok := err.(core.SessionNotFoundError); ok {
    apiH.Logger.Info(err1.Error())
    c.JSON(404, gin.H{"message": err.Error()})
    return
} else if err != nil {
    apiH.Logger.Error(err.Error())
    c.JSON(500, gin.H{"message": "internal error"})
    return
}

err = core.MakeNASProxyRequest(apiH.HttpClient, nasproxyHost, nasproxyPort, authToken, attrs)

debugAttrs := fmt.Sprintf("%+p", attrs)
debugAttrs2 := fmt.Sprintf("%+v", attrs)
apiH.Logger.Info("attrs:"+ " " + debugAttrs, log.Field("type", "serve-request"))
apiH.Logger.Info("attr2:"+ " " + debugAttrs2, log.Field("type", "serve-request"))

我需要打印attrs,但我得到了这个输出:

"msg":"attrs: %!p(core.Attributes={+0xc000420460 <nil> <nil> +0xc000238c60 <nil> <nil> <nil> <nil> +0xc000238c98 <nil> <nil> <nil> <nil>})"
"msg":"attr2: {NASIPAddress:0xc000420460 NASIdentifier:<nil> NASIPv6Address:<nil> Username:0xc000238c60 NASPort:<nil> FramedIPAddress:<nil> CalledStationID:<nil> CallingStationID:<nil> AcctSessionID:0xc000238c98 AcctMultiSessionID:<nil> NASPortID:<nil> FramedInterfaceID:<nil> FramedIPv6Prefix:<nil>}"

如何打印出值?
谢谢帮助。

英文:

I'm not very expert of GoLang, but I need to run troubleshooting on a project.
Now I'm try to print the value of same variables, but I receive as output the address and not the value:

attrs := core.Attributes{}
	err = rtrV1.SetAttributesStruct(&amp;attrs, sessAttrs, session.NasIP, ipv4Session, ipv6Session)
	if err1, ok := err.(core.SessionNotFoundError); ok {
		apiH.Logger.Info(err1.Error())
		c.JSON(404, gin.H{&quot;message&quot;: err.Error()})
		return
	} else if err != nil {
		apiH.Logger.Error(err.Error())
		c.JSON(500, gin.H{&quot;message&quot;: &quot;internal error&quot;})
		return
	}

	err = core.MakeNASProxyRequest(apiH.HttpClient, nasproxyHost, nasproxyPort, authToken, attrs)

	debugAttrs := fmt.Sprintf(&quot;%+p&quot;, attrs)
	debugAttrs2 := fmt.Sprintf(&quot;%+v&quot;, attrs)    
	apiH.Logger.Info(&quot;attrs:&quot;+&quot; &quot;+debugAttrs, log.Field(&quot;type&quot;, &quot;serve-request&quot;))
	apiH.Logger.Info(&quot;attr2:&quot;+&quot; &quot;+debugAttrs2, log.Field(&quot;type&quot;, &quot;serve-request&quot;))

I need to print attrs but I receive this output:

&quot;msg&quot;:&quot;attrs: %!p(core.Attributes={+0xc000420460 &lt;nil&gt; &lt;nil&gt; +0xc000238c60 &lt;nil&gt; &lt;nil&gt; &lt;nil&gt; &lt;nil&gt; +0xc000238c98 &lt;nil&gt; &lt;nil&gt; &lt;nil&gt; &lt;nil&gt;})&quot;
&quot;msg&quot;:&quot;attr2: {NASIPAddress:0xc000420460 NASIdentifier:&lt;nil&gt; NASIPv6Address:&lt;nil&gt; Username:0xc000238c60 NASPort:&lt;nil&gt; FramedIPAddress:&lt;nil&gt; CalledStationID:&lt;nil&gt; CallingStationID:&lt;nil&gt; AcctSessionID:0xc000238c98 AcctMultiSessionID:&lt;nil&gt; NASPortID:&lt;nil&gt; FramedInterfaceID:&lt;nil&gt; FramedIPv6Prefix:&lt;nil&gt;}&quot;

How can print the value?
Thanks for the help

答案1

得分: 3

你可以使用深度漂亮打印器。导入:github.com/davecgh/go-spew/spew

示例:

spew.Dump(attrs)
英文:

you can use deep pretty printer. Import: github.com/davecgh/go-spew/spew

Example:

spew.Dump(attrs)

答案2

得分: 3

你可以使用fmt包或JSON编组来打印结构体的值...
例如:

import "fmt"

type Person struct {
	FirstName string
}

func main() {
	p := &Person{"mr1"}
	fmt.Printf("%+v", p)
}

或者

import (
	"encoding/json"
	"fmt"
)

type Person struct {
	FirstName string
}

func main() {
	p := &Person{"mr1"}
	s, _ := json.Marshal(p)
	fmt.Printf("%s\n", s)
}
英文:

you can use fmt package or JSON marshaling to print the struct values...
for examples

import &quot;fmt&quot;

type Person struct {
	FirstName string
}

func main() {
	p := &amp;Person{&quot;mr1&quot;}
	fmt.Printf(&quot;%+v&quot;, p)
}

or

import (
	&quot;encoding/json&quot;
	&quot;fmt&quot;
)

type Person struct {
	FirstName string
}

func main() {
	p := &amp;Person{&quot;mr1&quot;}
	s, _ := json.Marshal(p)
	fmt.Printf(&quot;%s\n&quot;, s)
}

huangapple
  • 本文由 发表于 2022年1月26日 00:24:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/70852148.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定