global const import ?

3,215 views
Skip to first unread message

vlya...@gmail.com

unread,
Feb 16, 2015, 10:40:49 AM2/16/15
to golan...@googlegroups.com
How can i declare cost so that it could be used from different packages?

I import my package "mypkg" from "main" :
in "mypkg"

package mypkg

const MY_GLOBAL_CONST 1.0

package main
...
import (
"mypkg"
"fmt"
)
..
main () {
Print(MY_GLOBAL_CONST)
}

If I try to compile "main", it gives me an error: "undefined MY_GLOBAL_CONST"
Is there something i have to do to export MY_GLOBAL_CONST into different package?
Is there something equivalent to header files in c?
Thanks,







Gao

unread,
Feb 16, 2015, 10:43:05 AM2/16/15
to golan...@googlegroups.com, vlya...@gmail.com
it should follow your package name "mypkg.MY_GLOBAL_CONST"

chris dollin

unread,
Feb 16, 2015, 10:47:19 AM2/16/15
to Victor L, golang-nuts
On 16 February 2015 at 15:40, <vlya...@gmail.com> wrote:
> How can i declare cost so that it could be used from different packages?
>
> I import my package "mypkg" from "main" :
> in "mypkg"
>
> package mypkg
>
> const MY_GLOBAL_CONST 1.0

(missing =)

> package main
> ...
> import (
> "mypkg"
> "fmt"
> )
> ..
> main () {
> Print(MY_GLOBAL_CONST)

fmt.Println(mypkg.MY_GLOBAL_CONST)

To use an imported identifier, prefix it with its package
name and a `.`.

Chris

who will not mention the way you could, but should not,
avoid doing this.

--
Chris "allusive" Dollin

Victor L

unread,
Feb 16, 2015, 11:16:28 AM2/16/15
to chris dollin, golang-nuts
who will not mention the way you could, but should not,
avoid doing this.
Why not?

Rick

unread,
Feb 16, 2015, 2:26:45 PM2/16/15
to golan...@googlegroups.com, vlya...@gmail.com
Unless you need to refer to your constants in other packages just declare them in package main. Otherwise you need to prefix the constant name by the package it is imported from as others have pointed out.

Yves Junqueira

unread,
Feb 16, 2015, 5:41:26 PM2/16/15
to Victor L, chris dollin, golang-nuts
He probably meant this sort of hackery:

package main

import . "fmt"

func main() {
Println("Hello, playground")
}


--
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...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

roger peppe

unread,
Feb 17, 2015, 5:15:22 AM2/17/15
to vlya...@gmail.com, golang-nuts
On 16 February 2015 at 15:40, <vlya...@gmail.com> wrote:
> How can i declare cost so that it could be used from different packages?
>
> I import my package "mypkg" from "main" :
> in "mypkg"
>
> package mypkg
>
> const MY_GLOBAL_CONST 1.0

It's worth mentioning that in Go, constants are not conventionally
declared in ALL_CAPS, but as normally capitalised names.

So we'd prefer:

package mypkg
const MyGlobalConst = 1.0

....

package main
import (
"mypkg"
"fmt"
)

func main() {
fmt.Println(mypkg.MyGlobalConst)
}
Reply all
Reply to author
Forward
0 new messages