Is it possible to export a variable from main package? If impossible, why?

787 views
Skip to first unread message

伊藤和也

unread,
Jan 23, 2019, 9:27:57 PM1/23/19
to golang-nuts
package main

var Number int = 100

func main() {

}

Ian Lance Taylor

unread,
Jan 23, 2019, 11:29:59 PM1/23/19
to 伊藤和也, golang-nuts
Yes, you can export a variable from the main package. It's not very
useful, since normally nothing will import the package, but you can do
it if you want.

Ian

伊藤和也

unread,
Jan 24, 2019, 12:36:20 AM1/24/19
to golang-nuts
I couldn't import "main" package to other packages to use the variable "Number" and  I try to use "Number" in main package of different .go file in the same directory but I couldn't.

2019年1月24日木曜日 11時27分57秒 UTC+9 伊藤和也:

Wagner Riffel

unread,
Jan 24, 2019, 12:52:37 AM1/24/19
to 伊藤和也, golang-nuts
packages have nothing to do with files, if you are on some directory you don't need to import any package, just use Number on other files

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

伊藤和也

unread,
Jan 24, 2019, 1:11:45 AM1/24/19
to golang-nuts
Now I tried to use "Number" in the same package "main" in a different .go file in the same directly but I got the compile error "undefined: Number".


2019年1月24日木曜日 11時27分57秒 UTC+9 伊藤和也:

Michael Banzon

unread,
Jan 24, 2019, 1:16:47 AM1/24/19
to 伊藤和也, golang-nuts
Can you provide the output you get and the source you are compiling?

If you could also include the command line input+output it would probably help.

/M
--

Francisco Dalla Rosa Soares

unread,
Jan 24, 2019, 1:27:25 AM1/24/19
to Michael Banzon, 伊藤和也, golang-nuts
Are you compiling both files together?

----a.go----
package main

var Number int
-------------
---b.go----
package main

import "fmt"

func main() {
Number = 1
fmt.Println(Number)
}
-------------

go build a.go b.go

伊藤和也

unread,
Jan 24, 2019, 1:40:37 AM1/24/19
to golang-nuts
What Francisco Dalla Rosa Soares says is right. I only compiled "b.go". Is it possible to import "main" package to other packages except "main"?

2019年1月24日木曜日 11時27分57秒 UTC+9 伊藤和也:

Michael Banzon

unread,
Jan 24, 2019, 1:48:10 AM1/24/19
to 伊藤和也, golang-nuts
The question is what you are trying to accomplish - if you want two main packages share variables you could make a third package and have both main packages import that.

/M
--

伊藤和也

unread,
Jan 24, 2019, 2:00:33 AM1/24/19
to golang-nuts
Is it possible?
package main

var Number int = 100
func main() {}

package hello

import "fmt"

func abc() {
fmt.Println(Number)
}
2019年1月24日木曜日 11時27分57秒 UTC+9 伊藤和也:

Sam Whited

unread,
Jan 24, 2019, 2:18:39 AM1/24/19
to 'Than McIntosh' via golang-nuts
No, if you simply try this you'll see that you get an error:

import "main" is a program, not an importable package

—Sam

Francisco Dalla Rosa Soares

unread,
Jan 24, 2019, 2:19:00 AM1/24/19
to 伊藤和也, golang-nuts
Kazuya,

That makes no sense.

The main package is the point of entry to an executable. This is where things happen.
You create packages that are able to do things and you use them in the main package to some purpose. 

It's going against to the reason why packages are there.

What you're asking is like saying that you want to make your Image Processing library depend on your program, when the program should depend on the library

--

伊藤和也

unread,
Jan 24, 2019, 2:26:38 AM1/24/19
to golang-nuts
OK. Now I understand. Thanks for all replies.

2019年1月24日木曜日 11時27分57秒 UTC+9 伊藤和也:

Borman, Paul

unread,
Jan 28, 2019, 10:55:13 AM1/28/19
to Michael Banzon, 伊藤和也, golang-nuts

Not sure if this has been answered.

 

Yes, you can make a global value public in the main package, but it is more or less meaningless as there is no way for another package to import your main package.  It is possible there is some way that it might happen in the testing infrastructure, but that is probably not what you are looking for.

 

I have sometimes made public values in package main, but simply to give them more visibility.

 

Why do you want to make a value public in the main package?  What problem are you trying to solve?

 

    -Paul

Reply all
Reply to author
Forward
0 new messages