FAQ
New to goLang, and getting the whole public variable thing down; however,
am having one issue... and not sure if it's possible. In the main.go file
(package main) I have a couple of public variables that are set using
command-line arguments at run-time. I want those variables accessible to
other packages. Sharing variables among packages are simple enough (after
importing it, use package.Variable); however, there doesn't seem to be a
way to import main (get error "...is a program, not an importable package).
How do I import and use (if possible) public variables from the main
package?

--
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

  • C Banning at Feb 4, 2016 at 7:55 pm
    You need a "'context" package say:

    <pre>
          package ctx

          type context struct {
                Var1 string
                Var2 bool
                Var3 *log.Logger
          }

          var Ctx context
    </pre>

    Then import ctx package into main and any other packages that need access
    to the variables. Set the values in your main init(); they'll be available
    to any other packages that have imported ctx.

    On Thursday, February 4, 2016 at 1:41:57 PM UTC-6, Eric Brown wrote:

    New to goLang, and getting the whole public variable thing down; however,
    am having one issue... and not sure if it's possible. In the main.go file
    (package main) I have a couple of public variables that are set using
    command-line arguments at run-time. I want those variables accessible to
    other packages. Sharing variables among packages are simple enough (after
    importing it, use package.Variable); however, there doesn't seem to be a
    way to import main (get error "...is a program, not an importable package).
    How do I import and use (if possible) public variables from the main
    package?
    --
    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.
  • Voidlogic at Feb 4, 2016 at 7:56 pm
    Assuming public = exported (Go doesn't have public/private), then your
    misunderstanding is that nothing can import the main package. Main is the
    root of the import tree;

    1. Main can import other packages
    2. Other packages can import other packages
    3. BUT nothing can import main
    On Thursday, February 4, 2016 at 1:41:57 PM UTC-6, Eric Brown wrote:

    New to goLang, and getting the whole public variable thing down; however,
    am having one issue... and not sure if it's possible. In the main.go file
    (package main) I have a couple of public variables that are set using
    command-line arguments at run-time. I want those variables accessible to
    other packages. Sharing variables among packages are simple enough (after
    importing it, use package.Variable); however, there doesn't seem to be a
    way to import main (get error "...is a program, not an importable package).
    How do I import and use (if possible) public variables from the main
    package?
    --
    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.
  • C Banning at Feb 4, 2016 at 8:01 pm
    You need a "'context" package say:

    ----------
          package ctx

          type context struct {
                Var1 string
                Var2 bool
                Var3 *log.Logger
          }

          var Ctx context
    ---------

    Then import ctx package into main and any other packages that need access
    to the variables. Set the values in your main init(); they'll be available
    to any other packages that have imported ctx, e.g.,
    Ctx.Var3.Println("demo") - assuming you use a "dot" import of ctx package -

    -------
          import (
               . "ctx"
          )
    --------

    On Thursday, February 4, 2016 at 1:41:57 PM UTC-6, Eric Brown wrote:

    New to goLang, and getting the whole public variable thing down; however,
    am having one issue... and not sure if it's possible. In the main.go file
    (package main) I have a couple of public variables that are set using
    command-line arguments at run-time. I want those variables accessible to
    other packages. Sharing variables among packages are simple enough (after
    importing it, use package.Variable); however, there doesn't seem to be a
    way to import main (get error "...is a program, not an importable package).
    How do I import and use (if possible) public variables from the main
    package?
    --
    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.
  • Florin Patan at Feb 5, 2016 at 9:12 am
    Please please please don't use dot imports outside of testing realms (and
    even them when you really really need them).

    The idea of using a struct to pass multiple variables is a good one however
    I believe it should be passed as an argument to a dedicated initializing
    function rather than using it from the exported value directly as then
    testing becomes much more easy to handle.
    On Thursday, February 4, 2016 at 9:01:24 PM UTC+1, C Banning wrote:

    You need a "'context" package say:

    ----------
    package ctx

    type context struct {
    Var1 string
    Var2 bool
    Var3 *log.Logger
    }

    var Ctx context
    ---------

    Then import ctx package into main and any other packages that need access
    to the variables. Set the values in your main init(); they'll be available
    to any other packages that have imported ctx, e.g.,
    Ctx.Var3.Println("demo") - assuming you use a "dot" import of ctx package -

    -------
    import (
    . "ctx"
    )
    --------

    On Thursday, February 4, 2016 at 1:41:57 PM UTC-6, Eric Brown wrote:

    New to goLang, and getting the whole public variable thing down; however,
    am having one issue... and not sure if it's possible. In the main.go file
    (package main) I have a couple of public variables that are set using
    command-line arguments at run-time. I want those variables accessible to
    other packages. Sharing variables among packages are simple enough (after
    importing it, use package.Variable); however, there doesn't seem to be a
    way to import main (get error "...is a program, not an importable package).
    How do I import and use (if possible) public variables from the main
    package?
    --
    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
postedFeb 4, '16 at 7:41p
activeFeb 5, '16 at 9:12a
posts5
users4
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase