LoginSignup
1
0

More than 3 years have passed since last update.

Go言語でWebAssemblyを使う際の型変換

Posted at

ちょっとつまづいたので、忘れないようにメモ代わりに書いておきます。
実行環境
OS:Windows 10 64bit
言語環境:go version go1.14.1
ブラウザ:Firefox バージョン: 74.0

index.html
該当部分だけ表示
 <body>
        <button type="submit" id="v1" onClick="action('100','result')">Action</button><br>
        <div id="result" >結果</div>
</body>

エラーになります
func act(this js.Value, i []js.Value) interface{} {

    value1 := i[0]
    value2 := i[0].String()
    value3 := i[0].Int()
    fmt.Println(reflect.TypeOf(value1)) //=> js.value
    fmt.Println(reflect.TypeOf(value2)) //=> String
    fmt.Println(reflect.TypeOf(value3)) //=> エラーとなる

    return nil
}

コンパイルは問題なく通りました。

これだと動く
func act(this js.Value, i []js.Value) interface{} {
    value1 := i[0].String()
    int1, _ := strconv.Atoi(value1)
    ~~~~~~~~~~
}

数値型で扱う際は、いったんStrign型に変更後、型変換をする必要があります。
要注意ですね。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0