Live data from Hacker News

Using C Dynamic Libraries In Go Programs

goinggo.net

1–10 of 12 posts

Re: Using C Dynamic Libraries In Go Programs

#3

I wonder why the author didn't just read() in bytes from STDIN_FILENO? No need to use stuff like fget*.

I think by default your terminal is in "cooked" mode, where stdin is buffered a line at a time. Try

  cat /dev/fd/0
then type some things including backspace and enter.

However, you don't need ncurses to change this behavior. termios is the lower-level C API. You can do it from Go with the syscall APIs but I suspect it might be frustrating to make portable. https://code.google.com/p/go/source/browse/ssh/terminal/util...

Re: Using C Dynamic Libraries In Go Programs

#4
Coming from a C background, I was pretty disgusted with the "hack" of adding the include files in a comment before 'import C'. But then, even though they look similar, Go doesn't treat comments like C does, where they're eliminated by the C preprocessor and completely invisible to the source.

Re: Using C Dynamic Libraries In Go Programs

#5

Coming from a C background, I was pretty disgusted with the "hack" of adding the include files in a comment before 'import C'. But then, even though they look similar, Go doesn't treat comments like C does, where they're eliminated by the C preprocessor and completely invisible to the source.

You use C! You have no right to complain about ugly hacks! (jk)

Re: Using C Dynamic Libraries In Go Programs

#6
post #3

I wonder why the author didn't just read() in bytes from STDIN_FILENO? No need to use stuff like fget*.

I think by default your terminal is in "cooked" mode, where stdin is buffered a line at a time. Try cat /dev/fd/0 then type some things including backspace and enter. However, you don't need ncurses to change this behavior. termios is the lower-level C API. You can do it from Go with the syscall APIs but I suspect it might be frustrating to make portable. https://code.google.com/p/go/source/browse/ssh/terminal/util..…

Yep, it's a bit gnarly. But at least linux/MacOS is straightforward: https://groups.google.com/d/msg/golang-nuts/yRkGpw624-I/htJt...

Re: Using C Dynamic Libraries In Go Programs

#7

Coming from a C background, I was pretty disgusted with the "hack" of adding the include files in a comment before 'import C'. But then, even though they look similar, Go doesn't treat comments like C does, where they're eliminated by the C preprocessor and completely invisible to the source.

This is because cgo is actually a preprocessor that generates the final C and Go code that is built. The tools are just good at hiding this. Personally, I wouldn't say this is a bad thing. You can do some powerful things with this, including using your own .c and .h files in your Go packages. For a while, the YAML package at launchpad.net/goyaml did this by using libyaml code directly instead of having to link to external libraries, though now it is pure Go.

Re: Using C Dynamic Libraries In Go Programs

#8
post #3

I wonder why the author didn't just read() in bytes from STDIN_FILENO? No need to use stuff like fget*.

I think by default your terminal is in "cooked" mode, where stdin is buffered a line at a time. Try cat /dev/fd/0 then type some things including backspace and enter. However, you don't need ncurses to change this behavior. termios is the lower-level C API. You can do it from Go with the syscall APIs but I suspect it might be frustrating to make portable. https://code.google.com/p/go/source/browse/ssh/terminal/util..…

There's a package that does that: https://github.com/nsf/termbox-go

Re: Using C Dynamic Libraries In Go Programs

#9
post #3

I wonder why the author didn't just read() in bytes from STDIN_FILENO? No need to use stuff like fget*.

I think by default your terminal is in "cooked" mode, where stdin is buffered a line at a time. Try cat /dev/fd/0 then type some things including backspace and enter. However, you don't need ncurses to change this behavior. termios is the lower-level C API. You can do it from Go with the syscall APIs but I suspect it might be frustrating to make portable. https://code.google.com/p/go/source/browse/ssh/terminal/util..…

Ah you're right, I was thinking TTY didn't buffer at all but it does line buffering. That is contrasted to regular buffering if it wasn't a TTY.
Post reply on HN