Skip to content
This repository was archived by the owner on Jul 18, 2022. It is now read-only.

Updated sample for Go 1.9 #1

Closed
wants to merge 1 commit into from
Closed

Updated sample for Go 1.9 #1

wants to merge 1 commit into from

Conversation

koen92
Copy link

@koen92 koen92 commented Nov 21, 2017

Hi,

Thank you for your example! It really helped me a lot to understand how to bind a Go lib to node-ffi.

However, your example was out of date for the current Go version (1.9). This PR fixes some issues that occur when running your example with the latest Go version.

@marcboeker
Copy link

@koen92 I've found a way to return a string without hitting the runtime error. Converting the string back to a CString results in a Buffer object on the JavaScript side:

//export Hi
func Hi(name string) *C.char {
	return C.CString(fmt.Sprintf("Hi %s", name))
}

Change the return type to string:

var hw = FFI.Library(hw_path, {
  Hi: ['string', [GoString]],
})

And convert the returned Buffer to a string:

var res = hw.Hi(NewGoString('John'))
console.log(res.toString())

@MrSaints
Copy link
Owner

Thanks for taking a crack at this @koen92.

I've updated the repository (see 98cf01d) to work with Go 1.9, and Node 7.9. I took a slightly different approach. It appears to work on my machine, but there is a chance it may not work on someone else's machine.

Basically, I opted to create a type that can handle the getting, and setting, without forcing the user to know about the internals / esoterics. It handles the GoString conversion on get, and on set.

@marcboeker may work, but I wanted to keep it simple, without having too much cognitive overhead from cgo knowledge.

Thanks again all.

@MrSaints MrSaints closed this Dec 13, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants