go

Is it possible to explicitly call an exported Go WebAssembly function from JS?

限于喜欢 提交于 2020-12-29 04:17:56
问题 Is it possible to call a Go WebAssembly function, other than main , in Javascript? Let me first show what I did. My Go functions are defined as follows: package main import "fmt" func main() { fmt.Println("it works!") } func add(a, b int) int { return a + b } I can only invoke the main function: const go = new Go(); const data = await fetch("http://localhost:3333/main.wasm"); const result = await WebAssembly.instantiateStreaming(data, go.importObject); go.run(result.instance); which returns

passing function pointer to the C code using cgo

穿精又带淫゛_ 提交于 2020-12-29 04:13:50
问题 Starting from Go v1.6 cgo changed the rules of passing pointers to the C code golang/go#12416. The example of invoking a dynamic Go callback from C code from the wiki doesn't work anymore. package main import ( "fmt" "unsafe" ) /* extern void go_callback_int(void* foo, int p1); // normally you will have to define function or variables // in another separate C file to avoid the multiple definition // errors, however, using "static inline" is a nice workaround // for simple functions like this

How do I convert a Go array of strings to a C array of strings?

谁都会走 提交于 2020-12-29 04:11:38
问题 I am using cgo in a project, and I want to export a function for use. Here's an example of what I want to achieve: package csplit import ( "C" "strings" ) //export Split /* The Split function takes two C strings, the second of which represents a substring to split on, and returns an array of strings. Example: Split("1,2", ",") // gives ["1", "2"] */ func Split(original *C.char, split *C.char) []*C.char { goResult := strings.Split(C.GoString(original), C.GoString(split)) cResult := make([]*C

How do I convert a Go array of strings to a C array of strings?

送分小仙女□ 提交于 2020-12-29 04:11:36
问题 I am using cgo in a project, and I want to export a function for use. Here's an example of what I want to achieve: package csplit import ( "C" "strings" ) //export Split /* The Split function takes two C strings, the second of which represents a substring to split on, and returns an array of strings. Example: Split("1,2", ",") // gives ["1", "2"] */ func Split(original *C.char, split *C.char) []*C.char { goResult := strings.Split(C.GoString(original), C.GoString(split)) cResult := make([]*C

Why can't I assign type's value to an interface implementing methods with receiver type pointer to that type?

蹲街弑〆低调 提交于 2020-12-29 04:04:22
问题 I am 2-days old in the world of Golang, and going through the go tour . I couldn't help but notice a peculiarity which I cannot seem to be able to come at terms with a proper reasoning. This code is running perfectly: package main import ( "fmt" "math" ) type Vertex struct{ X,Y float64 } type Abser interface{ Abs() float64 } func (v Vertex) Abs() float64{ //method with value receiver argument return math.Sqrt(v.X*v.X+v.Y*v.Y) } func main(){ var myVer Vertex = Vertex{3,4} var inter Abser inter

Why can't I assign type's value to an interface implementing methods with receiver type pointer to that type?

末鹿安然 提交于 2020-12-29 04:02:54
问题 I am 2-days old in the world of Golang, and going through the go tour . I couldn't help but notice a peculiarity which I cannot seem to be able to come at terms with a proper reasoning. This code is running perfectly: package main import ( "fmt" "math" ) type Vertex struct{ X,Y float64 } type Abser interface{ Abs() float64 } func (v Vertex) Abs() float64{ //method with value receiver argument return math.Sqrt(v.X*v.X+v.Y*v.Y) } func main(){ var myVer Vertex = Vertex{3,4} var inter Abser inter

passing function pointer to the C code using cgo

不想你离开。 提交于 2020-12-29 04:02:36
问题 Starting from Go v1.6 cgo changed the rules of passing pointers to the C code golang/go#12416. The example of invoking a dynamic Go callback from C code from the wiki doesn't work anymore. package main import ( "fmt" "unsafe" ) /* extern void go_callback_int(void* foo, int p1); // normally you will have to define function or variables // in another separate C file to avoid the multiple definition // errors, however, using "static inline" is a nice workaround // for simple functions like this

Why can't I assign type's value to an interface implementing methods with receiver type pointer to that type?

和自甴很熟 提交于 2020-12-29 04:02:08
问题 I am 2-days old in the world of Golang, and going through the go tour . I couldn't help but notice a peculiarity which I cannot seem to be able to come at terms with a proper reasoning. This code is running perfectly: package main import ( "fmt" "math" ) type Vertex struct{ X,Y float64 } type Abser interface{ Abs() float64 } func (v Vertex) Abs() float64{ //method with value receiver argument return math.Sqrt(v.X*v.X+v.Y*v.Y) } func main(){ var myVer Vertex = Vertex{3,4} var inter Abser inter

passing function pointer to the C code using cgo

我与影子孤独终老i 提交于 2020-12-29 04:01:57
问题 Starting from Go v1.6 cgo changed the rules of passing pointers to the C code golang/go#12416. The example of invoking a dynamic Go callback from C code from the wiki doesn't work anymore. package main import ( "fmt" "unsafe" ) /* extern void go_callback_int(void* foo, int p1); // normally you will have to define function or variables // in another separate C file to avoid the multiple definition // errors, however, using "static inline" is a nice workaround // for simple functions like this

passing function pointer to the C code using cgo

試著忘記壹切 提交于 2020-12-29 04:01:06
问题 Starting from Go v1.6 cgo changed the rules of passing pointers to the C code golang/go#12416. The example of invoking a dynamic Go callback from C code from the wiki doesn't work anymore. package main import ( "fmt" "unsafe" ) /* extern void go_callback_int(void* foo, int p1); // normally you will have to define function or variables // in another separate C file to avoid the multiple definition // errors, however, using "static inline" is a nice workaround // for simple functions like this