Import everything from a package - go

I'm wondering if there is any way to import the full contents of a package so that I don't have to prefix calls to things in the package with a package name?
For example, is there a way to replace this:
import "fmt"
func main() {
fmt.Println("Hello, world")
}
with this:
import "fmt"
func main() {
Println("Hello, world")
}

The Go Programming Language Specification
Import declarations
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.
For example,
package main
import . "fmt"
func main() {
Println("Hello, world")
}
Playground: https://play.golang.org/p/xl7DIxxMlU5
Output:
Hello, world

Related

Import func from subfolder

I'm trying to import a func from a subfolder.
I have a main package
package main
import (
...
)
func handleRequests() {
http.HandleFunc("/health", health)
...
}
func main() {
handleRequests()
}
And then I have a folder called health in which I have a file called health.go.
package health
import (
...
)
func health(writer http.ResponseWriter, _ *http.Request) {
...
}
What shall my import look like and how do I call my health func?
At this point, your import statement doesn't mean anything, because it health package has no exported function or variable. You should check out the scoping in Go from the language spec. From here
Maybe consider checking out go modules, since it is now the suggested way to handle any go program that has more than a file.
The quick answer is,
your health.go
package health
import (
...
)
func Handler(writer http.ResponseWriter, _ *http.Request) {
...
}
your main.go
package main
import (
github.com/blablabla/yourproject/health
)
func handleRequests() {
http.HandleFunc("/health", health.Handler)
...
}
func main() {
handleRequests()
}
You must name function starting from uppercase symbol ("Health" instead of "health").
For example: health (your case) is private declaration, and Health would be public.
The same principle with types and variables naming.

Is there a way to block the usage of a specific function from a package when linting in Go

Is there a semi-trivial way of blocking the usage of a specific function i.e fmt.Sprintf from a specific Go package when doing static analysis using something like golangci-lint or go/analysis.
Say you have package a:
package a
import "fmt"
func doStuff() {
hello := fmt.Sprintf("%s\n", "hello world") // blocked and will throw a linting error
fmt.Println(hello)
}
and package b:
package b
import "fmt"
func doMore() {
hello := fmt.Sprintf("%s\n", "hello world") // allowed
fmt.Println(hello)
}
And in package a the usage of fmt.Sprintf is banned.

Call a package's function without using its package name?

Example:
package "main"
import "fmt"
func main() {
fmt.Println("hey there")
}
Could be written:
package "main"
import blah "fmt"
func main() {
blah.Println("hey there")
}
But is there anyway to import fmt to achieve:
package "main"
import "fmt" // ???
func main() {
Println("hey there")
}
In C# by contrast, you can do this by using a static import (e.g., using static System.Console). Is this possible in Go?
Use the . (explicit period) import. The specification says:
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.
Example:
package main
import (
. "fmt"
)
func main() {
Println("Hello, playground")
}
The use of the explicit period is discouraged in the Go community. The dot import makes programs more difficult to read because it's unclear if a name is a package-level identifier in the current package or in an imported package.
Another option is to declare a package-level variable with a reference to the function. Use a type alias to reference types.
package main
import (
"fmt"
)
var Println = fmt.Println
type ScanState = fmt.ScanState // type alias
func main() {
Println("Hello, playground")
}

golang custom struct undefined, can't import correctly

I've got 2 sibling files: main and test_two. In each is the file main.go and test_two.go respectively. In one I've got a custom struct and in the other I want to run a function with that struct as a param. I'm getting the error "undefined: Struct".
package main
import "github.com/user/test_two"
type Struct struct {
Fn string
Ln string
Email string
}
func main() {
foo := new(Struct)
foo.Fn = "foo"
foo.Ln = "bar"
foo.Email = "foo#bar.com"
test_two.Fn(foo)
test_two.go:
package test_two
import (
"fmt"
)
func Fn(arg *Struct) {
fmt.Println(arg.Fn)
}
Some rules to live by:
Don't define types in main (usually)
Don't try to import main in other packages
Don't try to import both ways (import cycle)
Always import from a lower level into a higher one (so mypkg into main)
All folders are packages, put related data/functions in them and name them well
You probably want something like this:
app/main.go
app/mypkg/mypkg.go
with contents for main.go:
// Package main is your app entry point in main.go
package main
import (
"stackoverflow/packages/mypkg"
)
func main() {
foo := mypkg.Struct{
Fn: "foo",
Ln: "foo",
Email: "foo#bar.com",
}
mypkg.Fn(foo)
}
Contents for mypkg.go:
package mypkg
import (
"fmt"
)
type Struct struct {
Fn string
Ln string
Email string
}
func Fn(s Struct) {
fmt.Printf("func called with %v\n", s)
}

Golang import package inside package

Go structure:
|--main.go
|
|--users
|
|---users.go
The two files are very simple:
main.go:
package main
import "./users"
func main() {
resp := users.GetUser("abcde")
fmt.Println(resp)
}
users.go:
package users
import "fmt"
func GetUser(userTok string) string {
fmt.Sprint("sf")
return "abcde"
}
But it seems fmt is not accessible in main.go. When I try to run the program, it gives
undefined: fmt in fmt.Println
Anybody knows how to make fmt accessible in main.go?
You need to import fmt in main as well.
Simply write "fmt" in import() in main.go and it should run.
import(
"fmt"
"./users"
)

Resources