Created
February 11, 2025 16:17
-
-
Save AmirMahdyJebreily/d3a1c8968588a18c0b566f79b64dd123 to your computer and use it in GitHub Desktop.
A function that converts a golang 2d slice into a js 2d array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//go:build js && wasm | |
package main | |
import ( | |
"iter" | |
) | |
// func js.ValueOf(x any) js.Value | |
// ValueOf returns x as a JavaScript value: | |
// | Go | JavaScript | | |
// | ---------------------- | ---------------------- | | |
// | js.Value | [its value] | | |
// | js.Func | function | | |
// | nil | null | | |
// | bool | boolean | | |
// | integers and floats | number | | |
// | string | string | | |
// | []interface{} | new array | | |
// | map[string]interface{} | new object | | |
func Convert2dArrayToJsArray[T any](goArr [][]T) iter.Seq2[int, []interface{}] { | |
return func(yeild func(index int, items []interface{}) bool) { | |
for i := range goArr { | |
r := make([]interface{}, len(goArr[i])) | |
for j := range goArr[i] { | |
r[j] = goArr[i][j] | |
} | |
if !yeild(i, r) { | |
return | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment