Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/cgo: write C code in a more proper way #12031

Closed
StEvUgnIn opened this issue Aug 5, 2015 · 10 comments
Closed

cmd/cgo: write C code in a more proper way #12031

StEvUgnIn opened this issue Aug 5, 2015 · 10 comments

Comments

@StEvUgnIn
Copy link

Hello,
Just like C code is written in Go comments, an IDE implementation would bring problem for intellisense. I find the current notation ugly. From today on, I propose to implement old style Pascal comments to use C code with Cgo.

package cgoexample

(*
    #include <stdio.h>
    #include <stdlib.h>

    void myprint(char* s) {
        printf("%s", s);
    }
*)

import "C"
import "unsafe"

func Example() {
    cs := C.CString("Hello from stdio\n")
    C.myprint(cs)
    C.free(unsafe.Pointer(cs))
} 

What do you think ? I think it would be another good loan to Pascal.

Source
http://www.freepascal.org/docs-html/ref/refse2.html
http://www.math.uni-leipzig.de/pool/tuts/FreePascal/prog/node3.html

@StEvUgnIn StEvUgnIn changed the title Cgo : write C code in a more proper way Cgo: write C code in a more proper way Aug 5, 2015
@davecheney
Copy link
Contributor

I'm sorry we cannot change the language, including the two forms of comments it accepts, //, and /* */.

I feel that you can use the C style /* ... */ forms equally as well as the example you showed above. Note that C code must directly preceed the import "C" line, that is there must be no linebreak between the commentary and the import declaration.

@pablochacin
Copy link

I will second ANY proposal to make included C code unmistakably distinguishable from comments.
I Personally would prefer an explicit directive like

inline{
     //  C code goes here
}

@davecheney
Copy link
Contributor

@pablochacin C is not part of the Go language specification. cgo is implemented entirely as a preprocessor and the go tool on top of the regular go compiler.

@mikioh mikioh changed the title Cgo: write C code in a more proper way cmd/cgo: write C code in a more proper way Aug 5, 2015
@StEvUgnIn
Copy link
Author

@davecheney Why cannot Cgo pimp go language and add syntaxes. It is a preprocessor. So I don't think there could be a problem.

Using the C preprocessor in Go would be great too :

{$ifdef OS}
/* Go code */
{$endif}

@dominikh
Copy link
Member

dominikh commented Aug 6, 2015

Or you could just move the majority of your C code to actual .c files.

@StEvUgnIn
Copy link
Author

@dominikh Does anything happens if there's a comment in a .c file included in Go using cgo ?

Edit: Do not forget that almost C programmers use block comments /* */, so yes, it could create errors, but not if you use line comments //.

@ianlancetaylor
Copy link
Contributor

@dominikh All cgo cares about is what you put in the .go file. You can write
// #include "foo.h"
import "C"
and put anything you like in foo.h. You can still reference things declared in foo.h using the C. syntax--see the cgo docs.

@dominikh
Copy link
Member

dominikh commented Aug 7, 2015

@ianlancetaylor I assume your last comment was directed at @StEvUgnIn, not me?

@davecheney
Copy link
Contributor

@StEvUgnIn et al, this issue is marked as closed. Please continue the discussion on golang-nuts. Thanks.

@ianlancetaylor
Copy link
Contributor

@dominikh Yes, sorry. Error on my part.

@golang golang locked and limited conversation to collaborators Aug 8, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants