Sprintf 在 js.Value 上显示 <number: 19> 而不是 19
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: %v", value.Get("age"))
fmt.Println(data)
我得到了:
Age: {[] 4626041242239631360 0}
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()
在包装 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 "" or "<T: V>" where T is v's type and V is a string representation of v's value.
使用Value.Int()
or Value.Float()
展开数值:
data := fmt.Printf("Age: %d", value.Get("age").Int())
这 {[] 4626041242239631360 0}
是 Value
结构本身及其内部结构的字符串表示形式。供参考:
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
}
js.Value
的文档显示 Get
的结果是另一个 Value
结构,而不是整数。因此,当您使用 %v 打印结果 js.Value
时,它会转到 js.Value
的默认格式化程序,它会打印 type 以及值。
当你明确告诉它把它打印成 %d 时,它会把 Value 结构打印成数字,在这种情况下意味着一个空数组一个 ref
结构和一个 *ref
指针,如你可以在代码中看到:
https://golang.org/src/syscall/js/js.go
您可能想要调用 Int()
方法,其中 returns 值为整数:
data := fmt.Sprintf("Age: %d, value.Get("Age").Int())
fmt.Println(data)
在我的代码中,我得到一个 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: %v", value.Get("age"))
fmt.Println(data)
我得到了:
Age: {[] 4626041242239631360 0}
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()
在包装 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 "" or "<T: V>" where T is v's type and V is a string representation of v's value.
使用Value.Int()
or Value.Float()
展开数值:
data := fmt.Printf("Age: %d", value.Get("age").Int())
这 {[] 4626041242239631360 0}
是 Value
结构本身及其内部结构的字符串表示形式。供参考:
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
}
js.Value
的文档显示 Get
的结果是另一个 Value
结构,而不是整数。因此,当您使用 %v 打印结果 js.Value
时,它会转到 js.Value
的默认格式化程序,它会打印 type 以及值。
当你明确告诉它把它打印成 %d 时,它会把 Value 结构打印成数字,在这种情况下意味着一个空数组一个 ref
结构和一个 *ref
指针,如你可以在代码中看到:
https://golang.org/src/syscall/js/js.go
您可能想要调用 Int()
方法,其中 returns 值为整数:
data := fmt.Sprintf("Age: %d, value.Get("Age").Int())
fmt.Println(data)