Is it possible don't specify package name? - go

Here is an example of my code:
package main
import (
"./bio"
)
func main() {
bio.PeptideEncoding(genome, codonTable)
}
Is it possible to use functions from my paxkage (bio) without specifying package name:
func main() {
PeptideEncoding(genome, codonTable)
}
?

You could use as an import declaration like:
. "./bio"
If an explicit period (.) appears instead of a name, all the package's exported identifiers declared in that package's package block will be declared in the importing source file's file block and must be accessed without a qualifier.
That is what a testing framework like govey does:
package package_name
import (
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func TestIntegerStuff(t *testing.T) {
Convey("Given some integer with a starting value", t, func() {
x := 1
Convey("When the integer is incremented", func() {
x++
Convey("The value should be greater by one", func() {
So(x, ShouldEqual, 2)
})
})
})
}
You don't need to use convey.So(), or convey.Convey() because of the import starting with a '.'.
Don't abuse it though, since, as twotwotwo comments, The style guide discourages it outside of tests.
Except for this one case, do not use import . in your programs.
It makes the programs much harder to read because it is unclear whether a name like Quux is a top-level identifier in the current package or in an imported package.
That is why I mentioned a testing framework using this technique.
As commented by Simon Whitehead, using relative import is not generally considered as the best practice (see for instance "Go language package structure").
You should also import the package via the GOPATH instead of relatively, as show in "Import and not used error".

Related

Golang in vscode: auto-import package

Imagine I want to use strconv.Atoi, but I am lazy, and want to import it automatically.
package main
import (
"fmt"
)
func main() {
i, _ := Atoi|("123")
fmt.Println(i)
}
The pipe sign (|) shows where my cursor is
In PyCharm I was able to automatically import the matching function via alt+ENTER.
Is there a way that vscode changes above code to this one:
package main
import (
"fmt"
"strconv"
)
func main() {
i, _ := strconv.Atoi("123")
fmt.Println(i)
}
VSCode won't guess which package Atoi() is from, but if you tell it, the import will be added automatically.
So just type
i, _ := strconv.Atoi("123")
And hit CTRL+S to save, and the import will be added automatically.
You may also press CTRL+ALT+O which is a shortcut to Organize Imports.
This is a reasonable compromise in Go. As part of API design, exported identifiers are created that read well with the package name. For example the constructor function that creates an MD5 hasher is md5.New() (and not for example md5.NewMD5()), the one that creates an SHA1 hasher is sha1.New(). Just entering New() it's often too verbose, and giving the package name is required to give context to what you refer to.

Directory Structure and Import

I do not understand how the package / project directory structure works.
I am following these 2 links enter link description here and enter link description here
My Go workspace is located under /workspace/golang.
My $GOPATH is equal to /workspace/golang
My directory structure is as follow :
/workspace/golang/src/Tutorial/
...tutorial_main.go <- Default 'Hello World' program
...library/
......arithmetic.go
Content of arithmetic.go :
package library
func addNum(a int, b int) int {
return a + b
}
I cd into library folder and ran go build arithmetic
Now, I cannot figure out how to use my arithmetic.go in my tutorial_main.go file.
I tried the following :
import "library"
fmt.Println("Result : ", library.addNum(1,4))
import "Tutorial/library"
fmt.Println("Result : ", library.addNum(1,4))
import "src/Tutorial/library"
fmt.Println("Result : ", library.addNum(1,4))
Neither works.
It keep saying it cannot find library
I don't understand what I am doing wrong.
With your setup, the package import path is:
import "Tutorial/library"
And you should capitalize the names you want to export in the library package so you can access them from other packages.
In general, the import path is the file path of the package (relative to $GOPATH) if it is local, or the remote path of the package, such as github.com/myaccount/package. The simple import names such as import library are reserved for built-in packages. Relative import paths also work, but they are not recommended, i.e. import ./library.
That said, with the module system $GOPATH is no longer used. I recommend you read modules and how you can work outside the $GOPATH.
In Go, your variables and functions that you'd like to export (make available outside your package) need to start with a capital letter.
package library
func privateAddNum(a int, b int) int {
return a + b
}
func PublicAddNum(a int, b int) int {
return a + b
}
privateAddNum is an unexported function and will only be accessible within the library package.
PublicAddNum is an exported function and will be accessible to external packages that import library.

How to export a name so that it becomes globally accessible?

I want to export a function from my package so that I can use it without typing a package name before it, how to do that?
import "mypackage"
func main() {
mypackage.myfunc() <-- that's what I have already
myfunc() <-- that's what I need
}
You can use one of the followings:
import (
. "mypackage" // without a name
mp "my/other/package" // rename
_ "my/totally/diffrent/package" // import a package solely for its side-effects (initialization)
)
Obviously, this pattern is not recommended since it can cause name conflicts with other packages.
Check out the dot imports bulletin

How to properly import a package from sub-directory in Golang?

I am pretty new to Golang and trying to make a simple REST api app work.
Initially, everything was all fine since I had all code in the same directory under the main package.
But, now I am at a stage where I need to start refactoring code into sub-directories and packages. Unfortunately, I have not been able to compile the app successfully.
My GOPATH is set to: ~/.workspace
The current app is at: ~/.workspace/src/gitlab.com/myapp/api-auth
This is how my current code organization is:
Here is my main.go
package main
import (
"net/http"
"os"
"strings"
"github.com/gorilla/context"
"github.com/justinas/alice"
"gopkg.in/mgo.v2"
"gitlab.com/myapp/api-auth/middlewares"
)
func main() {
privateKey := []byte(strings.Replace(os.Getenv("JWT_KEY"), "\\n", "\n", -1))
conn, err := mgo.Dial(os.Getenv("MONGO_CONN"))
if err != nil {
panic(err)
}
defer conn.Close()
conn.SetMode(mgo.Monotonic, true)
ctx := appContext{
conn.DB(os.Getenv("MONGO_DB")),
privateKey,
}
err = ctx.db.C("users").EnsureIndex(mgo.Index{
Key: []string{"username"},
Unique: true,
Background: true,
Sparse: false,
})
if err != nil {
panic(err)
}
commonHandlers := alice.New(LoggingHandler, context.ClearHandler, RecoveryHandler, AcceptHandler, ContentTypeHandler)
router := NewRouter()
router.Post("/users", commonHandlers.Append(BodyParserHandler(UserResource{})).ThenFunc(ctx.userCreationHandler))
router.Post("/sessions", commonHandlers.Append(BodyParserHandler(UserResource{})).ThenFunc(ctx.sessionCreationHandler))
http.ListenAndServe(":8080", router)
}
type appContext struct {
db *mgo.Database
privateKey []byte
}
Here is one of the middleware accept.go (Rest of the middleware are constructed similarly)
package middlewares
import "net/http"
// AcceptHandler ensures proper accept headers in requests
func AcceptHandler(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("Accept") != "application/vnd.api+json" {
writeError(w, errNotAcceptable)
return
}
next.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}
This is the error I get when I run go build from root of my app.
# gitlab.com/utiliti.es/api-auth
./main.go:11: imported and not used: "gitlab.com/myapp/api-auth/middlewares"
./main.go:42: undefined: LoggingHandler
./main.go:42: undefined: RecoveryHandler
./main.go:42: undefined: AcceptHandler
./main.go:42: undefined: ContentTypeHandler
./main.go:45: undefined: BodyParserHandler
./main.go:46: undefined: BodyParserHandler
The Go Programming Language Specification
Qualified identifiers
A qualified identifier is an identifier qualified with a package name
prefix. Both the package name and the identifier must not be blank.
QualifiedIdent = PackageName "." identifier .
A qualified identifier accesses an identifier in a different package,
which must be imported. The identifier must be exported and declared
in the package block of that package.
math.Sin // denotes the Sin function in package math
Import declarations
An import declaration states that the source file containing the declaration depends on functionality of the imported package (§Program
initialization and execution) and enables access to exported
identifiers of that package. The import names an identifier
(PackageName) to be used for access and an ImportPath that specifies
the package to be imported.
ImportDecl = "import" ( ImportSpec | "(" { ImportSpec ";" } ")" ) .
ImportSpec = [ "." | PackageName ] ImportPath .
ImportPath = string_lit .
The PackageName is used in qualified identifiers to access exported identifiers of the package within the importing source file.
It is declared in the file block. If the PackageName is omitted, it
defaults to the identifier specified in the package clause of the
imported package. If an explicit period (.) appears instead of a name,
all the package's exported identifiers declared in that package's
package block will be declared in the importing source file's file
block and must be accessed without a qualifier.
The interpretation of the ImportPath is implementation-dependent but it is typically a substring of the full file name of the compiled
package and may be relative to a repository of installed packages.
Implementation restriction: A compiler may restrict ImportPaths to non-empty strings using only characters belonging to Unicode's L, M,
N, P, and S general categories (the Graphic characters without spaces)
and may also exclude the characters !"#$%&'()*,:;<=>?[]^`{|} and the
Unicode replacement character U+FFFD.
Assume we have compiled a package containing the package clause package math, which exports function Sin, and installed the compiled
package in the file identified by "lib/math". This table illustrates
how Sin is accessed in files that import the package after the various
types of import declaration.
Import declaration Local name of Sin
import "lib/math" math.Sin
import m "lib/math" m.Sin
import . "lib/math" Sin
An import declaration declares a dependency relation between the importing and imported package. It is illegal for a package to import
itself, directly or indirectly, or to directly import a package
without referring to any of its exported identifiers. To import a
package solely for its side-effects (initialization), use the blank
identifier as explicit package name:
import _ "lib/math"
The error
./main.go:11: imported and not used: "gitlab.com/myapp/api-auth/middlewares"
says that you have no uses of package middlewares in package main, which is true.
The error
./main.go:42: undefined: AcceptHandler
says that you haven't defined AcceptHandler in package main, which is true.
"A qualified identifier is an identifier qualified with a package name prefix. A qualified identifier accesses an identifier in a different package, which must be imported."
For example, in package main, use the qualified identifier middlewares.AcceptHandler, which is a use of import "gitlab.com/myapp/api-auth/middlewares".

Package selection in Go

I'm trying to write an application to pull status from a database, but I seem to be getting stuck on a really basic principle of the language. I have the program written, but it doesn't compile due to the error use of package time not in selector.
A really basic example (from play.golang.org's own test environment)
package main
import (
"fmt"
"time"
)
func main() {
s_str := time.Now()
fmt.Println( printT(s_str) )
}
func printT(t time) time {
return t.Add(100)
}
Unfortunately, I've found documentation and helpdocs online a bit wanting. My understanding is that the import statement should include the library for the entire program like in C++ correct?
You have to prefix the imported types or variables with the name you gave to the package in the import (here you use the default name, that is "time"). That's what you did for the function Now but you have to do it also for the types.
So the type isn't time but time.Time (that is : the type Time that is declared in the package you import with the name "time").
Change your function to
func printT(t time.Time) time.Time {
return t.Add(100)
}
And for your second question : No, the import statement doesn't include the library for the entire program but only for the current file.

Resources