FAQ
Hi,

Newbie question here:


I am new to Golang but have used Ruby and Java for quite some time. One
thing I am struggling with that is simple to understand in other languages
is how to I use my code that is within my own project.

Golang allows you to import statements for external code and reference them
with packagename.Method but how exactly do I import code from within my own
project that I would consider helper code or internal library code.

Example:

1. create fileA with AwesomeMethod()
2. create fileB with AwesomeMethod()
3. create fileC with AwesoomeMethod()
4. Keep things DRY
4. change fileA to use fileC.AwesomeMethod()
5. change fileB to use fileC.AwesomeMethod()

How exactly do I do this? In ruby I would require the file and and
possibly use a Namespace to reference that method or include the module.
What do I need to do with Go? Where is this documented as I only see
references to using external packages.

Example repos would be great.

thanks,

Corey

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Shawn Milochik at Mar 26, 2015 at 9:30 pm
    If they're in the same program, you don't need to do anything -- just use
    the function. Running "go build" with the following files results in a
    working program.

      $ cat a.go
    package main

    import "fmt"

    func main() {
             fmt.Println(greet())
    }

      $ cat b.go
    package main

    func greet() string {
             return "hello"
    }

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Cmang at Mar 26, 2015 at 9:36 pm
    To be exceedingly clear, if the functions are a part of the same *package* you
    can just use the function. A program might import several packages but
    within the package boundary, all defined symbols are visible.
    On Thursday, March 26, 2015 at 2:30:57 PM UTC-7, Shawn Milochik wrote:

    If they're in the same program, you don't need to do anything -- just use
    the function. Running "go build" with the following files results in a
    working program.

    $ cat a.go
    package main

    import "fmt"

    func main() {
    fmt.Println(greet())
    }

    $ cat b.go
    package main

    func greet() string {
    return "hello"
    }


    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Shawn Milochik at Mar 26, 2015 at 9:39 pm

    On Thu, Mar 26, 2015 at 5:35 PM, wrote:

    To be exceedingly clear, if the functions are a part of the same *package* you
    can just use the function. A program might import several packages but
    within the package boundary, all defined symbols are visible.

    On Thursday, March 26, 2015 at 2:30:57 PM UTC-7, Shawn Milochik wrote:

    If they're in the same program, you don't need to do anything -- just use
    the function. Running "go build" with the following files results in a
    working program.
    Thanks for the clarification. I meant what you said -- by "same program" I
    meant "both in the 'main' package," but I wasn't explicit. The original
    question was explicit in referring to not using packages, so I was
    responding in that context.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Corey Osman at Mar 26, 2015 at 10:12 pm
    $ cat a.go
    package main

    import "fmt"

    func main() {
             fmt.Println(greet())
    }

    $ cat b.go
    package main

    func greet() string {
             return "hello"
    }

    $ go run a.go
    # command-line-arguments
    ./a.go:6: undefined: greet


    Am I missing something?

    On Thursday, March 26, 2015 at 2:30:57 PM UTC-7, Shawn Milochik wrote:

    If they're in the same program, you don't need to do anything -- just use
    the function. Running "go build" with the following files results in a
    working program.

    $ cat a.go
    package main

    import "fmt"

    func main() {
    fmt.Println(greet())
    }

    $ cat b.go
    package main

    func greet() string {
    return "hello"
    }


    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Shawn Milochik at Mar 26, 2015 at 10:14 pm
    Yes, you have to either:

    go build # then run executable

    or

    go run a.go b.go

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Corey Osman at Mar 26, 2015 at 10:30 pm
    $ go build a.go
    # command-line-arguments
    ./a.go:6: undefined: greet

    $ go run a.go b.go
    hello

    $ go build a.go b.go
    $ ./a
    hello

    $ go version
    go version go1.3 darwin/amd64

    So based on this information below if I want to build something I have to
    build all the files at once. I can't build one file and expect it to
    include all the other files that it needs.
    Is this correct?


    Corey
    On Thursday, March 26, 2015 at 3:14:24 PM UTC-7, Shawn Milochik wrote:

    Yes, you have to either:

    go build # then run executable

    or

    go run a.go b.go

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Shawn Milochik at Mar 26, 2015 at 10:50 pm

    On Thu, Mar 26, 2015 at 6:30 PM, Corey Osman wrote:

    So based on this information below if I want to build something I have to
    build all the files at once. I can't build one file and expect it to
    include all the other files that it needs.
    Is this correct?

    Yes. Generally you don't specify all the files in "go build" -- you just
    run "go build" in your project directory and it names the executable the
    name of the directory and automatically uses all* the .go files.

    If you haven't already, check out this and/or the screencast linked to on
    this page:
    https://golang.org/doc/code.html


    *Not technically true, since it doesn't compile in test files and
    platform-specific files if you have build flags, but that's off topic.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Carlos Castillo at Mar 26, 2015 at 10:54 pm
    You should not build programs by specifying files.

    eg:

    go build

    or

    go install pkgname



    not:

    go build a.go b.go


    Learn how to create and use packages here http://golang.org/doc/code.html

    Also, you should generally never use go run for anything but small
    single-file programs.
    On Thursday, March 26, 2015 at 3:30:02 PM UTC-7, Corey Osman wrote:

    $ go build a.go
    # command-line-arguments
    ./a.go:6: undefined: greet

    $ go run a.go b.go
    hello

    $ go build a.go b.go
    $ ./a
    hello

    $ go version
    go version go1.3 darwin/amd64

    So based on this information below if I want to build something I have to
    build all the files at once. I can't build one file and expect it to
    include all the other files that it needs.
    Is this correct?


    Corey
    On Thursday, March 26, 2015 at 3:14:24 PM UTC-7, Shawn Milochik wrote:

    Yes, you have to either:

    go build # then run executable

    or

    go run a.go b.go

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Volker Dobler at Mar 26, 2015 at 11:10 pm

    Am Donnerstag, 26. März 2015 23:30:02 UTC+1 schrieb Corey Osman:
    [...]
    So based on this information below if I want to build something I have to
    build all the files at once. I can't build one file and expect it to
    include all the other files that it needs.
    Is this correct?
    No.

    In Go you build (or import or test) packages, not files.

    A package may consist of several files but managing them
    is the job of the go tool (not your's).
    The go tool works with packages only, with one exception:
    'go run' which is a cute way to run quick tests and one-file script.

    You should 'go build', 'go test' and 'go install' your code.

    V.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedMar 26, '15 at 9:26p
activeMar 26, '15 at 11:10p
posts10
users5
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase