Giter Site home page Giter Site logo

Generate "import C" for cgo about jennifer HOT 10 CLOSED

dave avatar dave commented on May 19, 2024
Generate "import C" for cgo

from jennifer.

Comments (10)

dave avatar dave commented on May 19, 2024

No, you're right - there's no support for this, but definitely something I'll add. I'll have to have a read up on the syntax to make a call on how to make it happen...

from jennifer.

dave avatar dave commented on May 19, 2024

How about:

  • "C" import is added by simply using Qual("C", "foo") in your code, as is the case for all other imports.
  • It's treated as a special case when the page renders
  • The Qual method always renders the upper-case "C" (as is convention).
  • Imports are usually aliased - with "C" the alias will be suppressed (not sure if this is a requirement?).
  • The import is separated below the other imports.
  • A new method CommentC() is added with the same functionality as "Comment()". The comment (or comments) are rendered directly above the import... So you'd get something like:
import (
    fmt "fmt"
    baz "foo.bar/baz"
)

// foo
/*
bar
baz
*/
import "C"

func main() {
    fmt.Println(C.create_object(), baz.Quz)
}

from jennifer.

pekim avatar pekim commented on May 19, 2024

That all sounds good to me.

Imports are usually aliased - with "C" the alias will be suppressed (not sure if this is a requirement?).

I'm not sure either, but I've never seen an example of cgo usage with the import aliased. Suppressing the alias sounds like the right thing to do.

from jennifer.

dave avatar dave commented on May 19, 2024

Yes the only reason we always alias the imports is because we don't actually know the package name, so dropping the alias on "C" is perfectly fine. I should have some time tomorrow to implement this.

from jennifer.

dave avatar dave commented on May 19, 2024

Hey - a question: Are there times where you might want to import the "C" pseudo-package, but not reference it in code?

e.g.:

package foo

import "C"

func main() {
    // never use C
}

In this case would you use the import _ "C" syntax?

from jennifer.

pekim avatar pekim commented on May 19, 2024

If there were to be such a scenario then it would likely be to control a compiler or linker flag, and the import would be preceded by some comment lines.

// #cgo LDFLAGS: -lpng
import "C"

I don't think that I'm likely to need this in my current use cases. But I can't rule it out.

I'm not sure whether import _ "C" would be recognised by the compiler for cgo purposes. I would need to try it with a simple use case. Unfortunately I won't be in a position to do that for about 10 or 11 hours.

from jennifer.

dave avatar dave commented on May 19, 2024

Yes it looks like "C" is indeed a special case, and can be imported without a "_" even without being used...

screen shot 2018-01-10 at 10 52 52

from jennifer.

dave avatar dave commented on May 19, 2024

I'm happy with this... Let me know if you think it needs any tweaks.

from jennifer.

dave avatar dave commented on May 19, 2024

BTW @pekim thanks very much for the excellent suggestion, and feel free to open issues if you have any more questions. I'm really glad you're enjoying my work!

from jennifer.

pekim avatar pekim commented on May 19, 2024

I've just tried it out, and it works nicely. Thank you.

I appreciate that you tag releases, including v0.17.0 with these changes.

from jennifer.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.