import - How do packages work in golang -
i trying understand how packages work in go improve in terms of golang enforces rather done or considered practice (we can talk practice later too, wish understand go first).
from effective go says:
"another convention bundle name base of operations name of source directory..."
however, description above doesn't seem forced go or required. thus, wondering, if allowed have multiple files different bundle declarations @ top in same directory base. if allowed have multiple bundle declaration in same directory, how import them , utilize each 1 separately in same file? basically, guess 1 of problem have due wording of of go tutorial/documentation. if convention, me, implied not enforced language. because example, go programmers not write keyword func
function convention. write func
because otherwise go yell @ , not compile. thus, wish clarify illustration bellow (and if possible alter documentation go, because big deal in opinion, how can done?).
for illustration have 3 file a.go
, b.go
, c.go
print print function print()
prints a,b,c respectively. on same base of operations directory called maybe base
. each has different bundle declaration package apkg, bundle bpkg, bundle cpkg
.
how go , import them? 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 nameb.print() \\prints b namec.print() \\prints c }
or maybe don't need specify name if bundle statments top different:
package main import( "github.com/user_me/base" ) func main() { apkg.print() \\prints bpkg.print() \\prints b cpkg.print() \\prints c }
the printing file are:
a.go:
//file @ github.com.user_me/base , name a.go bundle apkg import "fmt" func print(){ fmt.println("a") }
b.go:
//file @ github.com.user_me/base , name b.go bundle bpkg import "fmt" func print(){ fmt.println("b") }
c.go: //file @ github.com.user_me/base , name c.go bundle cpkg
import "fmt" func print(){ fmt.println("c") }
also, if can have name different base
, clarify me how import done? if bundle name package apkg
in base of operations import have 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 soon. importing deal in go has been little confusing me , love reply , share world.
no, it's 1 bundle per folder, have 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 build otherwise:
┌─ oneofone@oa [/t/blah] └──➜ go build can't load package: bundle .: found packages pkga (blah1.go) , pkgb (blah2.go) in /tmp/blah
your bundle name doesn't need have same name directory in, files in 1 directory must have same bundle name.
also, can rename packages on import, example:
import ( cr "crypto/rand" mr "math/rand" )
or library called main (bad thought btw) :
import m "github.com/user_me/base/main"
import go package
No comments:
Post a Comment