Sprintf在js.Value上显示<number: 19>而不是19。

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

Sprintf shows <number: 19> instead of 19 on js.Value

问题

在我的代码中,我得到了一个js.Value,它是一个简单的对象{"Age":19}

为了显示它,我使用了以下代码:

data := fmt.Sprintf("Age: %v", value.Get("age"))
fmt.Println(data)

但输出结果是:

Age: <number: 19> 

期望的输出结果是:

Age: 19 

当我将%v替换为%d,代码如下:

data := fmt.Sprintf("Age: %d", value.Get("age"))
fmt.Println(data)

我得到了以下输出结果:

Age: {[] 4626041242239631360 0}
英文:

In my code I'm getting a js.Value that is a simple object {&quot;Age&quot;:19}

For displaying it, I used:

data := fmt.Sprintf(&quot;Age: %v&quot;, value.Get(&quot;age&quot;))
fmt.Println(data)

But the output was:

Age: &lt;number: 19&gt; 

Expected output is:

Age: 19 

Once I replaced the %v by %d to be:

data := fmt.Sprintf(&quot;Age: %v&quot;, value.Get(&quot;age&quot;))
fmt.Println(data)

I got:

Age: {[] 4626041242239631360 0}

答案1

得分: 3

js.Value.Get 返回一个 js.Value,而 js.Value 实现了 fmt.Stringer 接口。你看到的输出结果是在一个包装了 js.TypeNumber 的实例上调用 Value.String() 的结果。

> String 将值 v 转换为字符串。
String 是一个特殊情况,因为 Go 的 String 方法约定。与其他的获取器不同,如果 v 的类型不是 TypeString,它不会引发 panic。相反,它返回一个字符串,其形式为“<T>”或“<T: V>”,其中 T 是 v 的类型,V 是 v 值的字符串表示。

使用 Value.Int()Value.Float() 来获取数值:

data := fmt.Printf("Age: %d", value.Get("age").Int())

<hr>

{[] 4626041242239631360 0}Value 结构体本身及其内部的字符串表示。供参考:

type Value struct {
	_     [0]func() // 不可比较;用于使 == 无法编译通过
	ref   ref       // 标识 JavaScript 值,参见 ref 类型
	gcPtr *ref      // 在 Value 不再被引用时触发终结器的指针
}
英文:

js.Value.Get returns a js.Value again, and js.Value implements fmt.Stringer interface. The output you see is the result of calling Value.String() on an instance that wraps js.TypeNumber

> String returns the value v as a string.
String is a special case because of Go's String method convention. Unlike the other getters, it does not panic if v's Type is not TypeString. Instead, it returns a string of the form "<T>" or "<T: V>" where T is v's type and V is a string representation of v's value.

Use Value.Int() or Value.Float() to unwrap the numerical value:

data := fmt.Printf(&quot;Age: %d&quot;, value.Get(&quot;age&quot;).Int())

<hr>

This instead {[] 4626041242239631360 0} is the string representation of the Value struct itself with its internals. For reference:

type Value struct {
	_     [0]func() // uncomparable; to make == not compile
	ref   ref       // identifies a JavaScript value, see ref type
	gcPtr *ref      // used to trigger the finalizer when the Value is not referenced any more
}

答案2

得分: 2

js.Value的文档显示,Get方法的结果是另一个Value结构体,而不是整数。所以当你用%v打印结果的js.Value时,它会使用默认的js.Value格式化程序,打印出类型和值。

当你明确告诉它用%d打印时,它会将Value结构体打印为数字,这在这种情况下意味着一个空数组、一个ref结构体和一个*ref指针,你可以在这段代码中看到:
https://golang.org/src/syscall/js/js.go

你可能想要调用Int()方法,将值作为整数返回:

data := fmt.Sprintf("Age: %d", value.Get("Age").Int())
fmt.Println(data)
英文:

The documentation for js.Value shows that the result of Get is another Value struct, and not an integer. So when you print the resulting js.Value with %v, it goes to the default formatter for a js.Value, which prints the type as well as the value.

When you explicitly told it to print it as %d, it prints the Value struct as numbers, which in this case means an empty array a ref struct and a *ref pointer, as you can see in the code:
https://golang.org/src/syscall/js/js.go

What you probably want is to call the Int() method, which returns the value as an integer:

data := fmt.Sprintf(&quot;Age: %d, value.Get(&quot;Age&quot;).Int())
fmt.Println(data)

huangapple
  • 本文由 发表于 2021年11月17日 17:39:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/70001977.html
匿名

发表评论

匿名网友

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

确定