I need help on how to use packages in go. I am new to go programming.
You can start by reading “Organizing Go code” (talk, article).
Are you having any specific problems?
I have main.go file in a directory called Hashing. In Hashing directory there is another directory called modulus which contains modulus.go file. In package declaration of modulus.go, i have declared “package modulus” and similarly in main.go file i have declared “package main”. Whenever i try to run main.go, it runs fine but modulus.go gives error after running as “multiple packages in go-lang”.
I tried searching abt the issue online but no luck.
So you have a directory that looks like this:
hashing/main.go
(File starts with package main
)
hashing/modulus/modulus.go
(File starts with package modulus
)
Then while you are in hashing/
you do go build
. This will create a binary called hashing
which you can execute with ./hashing
.
Is the above correct?
Which operating system are you using by the way?
I do not understand what do you mean by modulus.go
running. If modulus.go
starts with package modulus
then it cannot run because it is a package. Packages get used by other code that can run (like main). So in your main.go
somewhere you would have a function call like modulus.FunctionName()
so that main can use your modulus package.
Notice that you cannot have 2 different packages in the same folder.
Thanks nesigma, i ran go build in hashing folder and removed “package modulus” line and added “package main” in hashing/modulus.go file also. The reason it was giving error was modulus.go and main.go both were having main() function.
Only i am little bit of confused about, why package main is to be declared when i am using func main().?
@Tanmay_Shirsath, Package name is the package’s default name for imports. (All files in a package must use the same name.) Here is the link that can help you to start with Golang: https://golang.org/doc/code.html#Command
Thanks adriel
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.