在 wasm 构建中使用可选参数
时间:2023-04-14
浏览:245
作者:佚名
欢迎各位小伙伴来到golang学习网,相聚于此都是缘哈哈哈!今天我给大家带来《在 wasm 构建中使用可选参数》,这篇文章主要讲到golang等等知识,如果你对Golang相关的知识非常感兴趣或者正在自学,都可以关注我,我会持续更新相关文章!当然,有什么建议也欢迎在评论留言提出!一起学习!
问题内容
我正在尝试将以下代码从 IndexedDB JavaScript API 转换为 GO WASM:
request.onupgradeneeded = function(event) {
var result = event.target.result;
var objectStore = result.createObjectStore("employee", {keyPath: "id"});
objectStore.add({ id: "00-01", name: "Karam", age: 19, email: "kenny@planet.org" });
}
所以,我写道:
var dbUpgrade js.Func
var result, request js.Value
dbUpgrade = js.FuncOf(func(this js.Value, args []js.Value) interface{} {
defer dbUpgrade.Release()
result = this.Get("result")
var objectStore = result.Call("createObjectStore", "employee")
objectStore.Call("add", `{ id: "00-01", name: "Karam", age: 19, email: "kenny@planet.org" }`)
window.Call("alert", "First record posted.")
return nil
})
request.Set("onupgradeneeded", dbUpgrade)
但我得到了以下运行时错误:
panic: JavaScript error: Failed to execute 'add' on 'IDBObjectStore': The object store uses out-of-line keys and has no key generator and the key parameter was not provided.
我知道原因是因为不包括{keyPath: "id"}atresult.Call("createObjectStore", "employee"以匹配
JavaScript,但我尝试了以下方法但没有任何效果:
// 1.
result.Call("createObjectStore", "employee", "{keyPath: `id`}")
// 2.
key, _ := json.Marshal(map[string]string{"keyPath": "id"})
result.Call("createObjectStore", "employee", key)
// 3.
result.Call("createObjectStore", `"employee", "{keyPath: "id"}"`)
有没有想过怎么做?
正确答案
基于以下文档ValueOf:
https://pkg.go.dev/syscall/js@go1.17.3#ValueOf
这应该有效:
result.Call(...,map[string]interface{}{"keyPath": "id"})
今天关于《在 wasm 构建中使用可选参数》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于golang的内容请关注golang学习网公众号!
本文内容来源于互联网,如有侵权请联系删除。
近期热门
11.18
11.15
站内热门
最新文章
11.28
热门专题
