Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do packages work in golang

Tags:

import

package

go

I was trying to understand how packages work in go better in terms of what golang actually enforces rather than what is usually done or considered good practice (we can talk about good practice later too, but I wish to understand go first).

From effective go it says:

"Another convention is that the package name is the base name of its source directory..."

However, the description above doesn't seem to be forced by go or required. Thus, I was wondering, if I was allowed to have multiple files with different package declarations at the top in the same directory base. If I am allowed to have multiple package declaration in the same directory, how do I then import them and use each one separately in the same file? Basically, I guess one of the problem I have is due to the wording of some of the go tutorial/documentation. If it is by convention, then for me, its implied that it is NOT enforced by the language. Because for example, go programmers do NOT write the keyword func for a function by convention. We write func because otherwise go will yell at you and it will not compile. Thus, I wish to clarify this with the example bellow (and if possible change the documentation for go, because this is a big deal in my opinion, how can this be done?).

For example say I have three file A.go, B.go, C.go that print a print function Print() that simply prints a,b,c respectively. They are all on the same base directory called maybe base. Then each has a different package declaration package Apkg, package Bpkg, package Cpkg.

how would you then go and import them? Would something as follow work?

package main

import(
    nameA "github.com/user_me/base/Apkg"
    nameB "github.com/user_me/base/Bpkg"
    nameC "github.com/user_me/base/Cpkg"
)

func main() {
    nameA.Print() \\prints a
    nameB.Print() \\prints b
    nameC.Print() \\prints c
}

or maybe we don't even need to specify the name if the package statments are the top are already different:

package main

import(
    "github.com/user_me/base"
)

func main() {
    Apkg.Print() \\prints a
    Bpkg.Print() \\prints b
    Cpkg.Print() \\prints c
}

the printing file are:

A.go:

//file at github.com.user_me/base and name A.go
package Apkg

import "fmt"

func Print(){
    fmt.Println("A")
}

B.go:

//file at github.com.user_me/base and name B.go
package Bpkg

import "fmt"

func Print(){
    fmt.Println("B")
}

C.go: //file at github.com.user_me/base and name C.go package Cpkg

import "fmt"

func Print(){
    fmt.Println("C")
}

Also, if you can have a name different from the base, could someone clarify to me how the import is actually done? If the package name is package Apkg in base does the import have to be import github.com/user_me/base or import github.com/user_me/base/Apkg or github.com/user_me/Apkg.

I have not yet tested this but I will do so soon. The importing deal in go has been a little confusing for me and would love to get an answer and share it with the world.

like image 841
Charlie Parker Avatar asked Jun 20 '14 19:06

Charlie Parker


People also ask

How does packaging work in Golang?

Go programs are organized into packages. A package is a collection of source files in the same directory that are compiled together. Functions, types, variables, and constants defined in one source file are visible to all other source files within the same package. A repository contains one or more modules.

How do you write packages in go?

Check your GOPATH in environment variables and set it to the directory which contains all Go files. Create a new folder with the name of the package you wish to create. In the folder created in step 2, create your go file that holds the Go package code you wish to create.

How many packages are there in Golang?

As we discussed, there are two types of packages. An executable package and a utility package.

How do I access packages in Golang?

Import package in Golang Here, we have used the import keyword to import the fmt package. func main() { // use the Println() function of fmt fmt. Println("Hello World!") }


1 Answers

No, it's 1 package per folder, so you would have to have them :

$GOPATH/src/github.com/user_me/base/Apkg/a.go
$GOPATH/src/github.com/user_me/base/Bpkg/b.go
$GOPATH/src/github.com/user_me/base/Cpkg/c.go

You can't even build it otherwise:

┌─ oneofone@Oa [/t/blah]                                                                                                         
└──➜ go build
can't load package: package .: found packages pkgA (blah1.go) and pkgB (blah2.go) in /tmp/blah

Your your package name doesn't need to have the same name as the directory they are in, however all files in one directory must have the same package name.

Also, you can rename packages on import, for example:

import (
   cr "crypto/rand"
   mr "math/rand"
)

Or your library is called main (bad idea btw) :

import m "github.com/user_me/base/main"
like image 168
OneOfOne Avatar answered Oct 19 '22 23:10

OneOfOne