Live data from Hacker News

C and Go without CGO

skullcountry.com

1–10 of 21 posts

Re: C and Go without CGO

#3
I'd love to know how this works behind the scenes.

In the article, a file called chello.c and a file called gohello.go are created - do the filenames have any significance? How does the go compiler and linker know what to do here?

Re: C and Go without CGO

#4

I'd love to know how this works behind the scenes. In the article, a file called chello.c and a file called gohello.go are created - do the filenames have any significance? How does the go compiler and linker know what to do here?

Run strace -f -eexecve go build to see whats going on under the covers. The filenames are because 6c and 6g both produced an object file named hello.6.

6g,6c, and 6a produce object files on amd64. 6l is the linker which combines them. Honestly I had a little trouble running the commands individually so I just used go build.

Re: C and Go without CGO

#7
post #5

"as fast as physically possible on your CPU architecture" is a ludicrous phrase full of misunderstanding.

Can you suggest a better one?

I think what pekk is complaining about is the “as fast as physically possible” - unless your CPU is on a rocket heading to the moon it is not physically moving as fast as physically possible.

As for a suggestion how about just “as fast as possible”.

Re: C and Go without CGO

#8
The article doesn't mention any of the downsides to this approach, the biggest being style of C code. From Dave Cheney[0]

- Using C code is inherently unsafe, not just because it unholsters all the C footguns, but because you can address any symbol in the runtime. With great power comes great responsibility.

- The Go 1 compatibility guarantee does not extend to C code.

- C functions cannot be inlined.

- Escape analysis cannot follow values passed into C functions.

- Code coverage does not extend to C functions.

- The C compilers (5c, 6c, 8c) are not as optimised as their companion Go compilers, you may find that the code generated is not as efficient as the same code in Go.

- You are writing plan 9 style C code, which is a rough analogue of C89.

[0]: http://dave.cheney.net/2013/09/07/how-to-include-c-code-in-y...

Re: C and Go without CGO

#10
post #9

Can anyone explain the use of the "·" character?

In go you use '.' for namespace accesses. Eg: os.NewFile. But in C '.' means access struct member. So the · is used instead so to do

    f = os.NewFile(3, "/dev/zero") # Go
you would use

    f = os·NewFile(3, "/dev/zero"); # C
Post reply on HN