Golang import package inside package - go

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"
)

Related

Name resolution problem in multi package go module

I'm trying to create a module that contains multiple packages but I don't understand why I'm getting errors.
Layout:
λ ~/code/go-test/src/mod-test/ » tree
.
├── cmd
│   └── a.go
├── go.mod
├── main.go
└── pkg
└── foo
└── b.go
3 directories, 4 files
main.go:
package main
import (
"fmt"
"github.com/go-test/mod-test/cmd"
)
func main() {
fmt.Println("main")
A()
}
cmd/a.go:
package cmd
import (
"fmt"
"github.com/go-test/mod-test/pkg/foo"
)
func A() {
fmt.Println("a")
B()
}
pkg/foo/b.go:
package foo
import "fmt"
func B() {
fmt.Println("B")
}
go.mod:
module github.com/go-test/mod-test
go 1.12
I get the following error:
λ ~/code/go-test/src/mod-test/ » go build
# github.com/go-test/mod-test/cmd
cmd/a.go:5:2: imported and not used: "github.com/go-test/mod-test/pkg/foo"
cmd/a.go:10:2: undefined: B
Can anyone help explain what I've done wrong and why I get the error?
Thanks,
There is bug in the cmd/a.go. You did not use "github.com/go-test/mod-test/pkg/foo". Also B() is under github.com/go-test/mod-test/pkg/foo package, so you have to specify it. See below:
package cmd
import (
"fmt"
"github.com/go-test/mod-test/pkg/foo"
)
func A() {
fmt.Println("a")
// B()
foo.B()
}
There is another way to avoid this. If you don't want to use the package name, simply just put a . before importing a package. By doing that you can call a public fn or use a public var of that package. After doing that your main.go and cmd/a.go files look like below:
main.go:
package main
import (
"fmt"
. "github.com/go-test/mod-test/cmd"
)
func main() {
fmt.Println("main")
A()
}
cmd/a.go:
package cmd
import (
"fmt"
. "github.com/go-test/mod-test/pkg/foo"
)
func A() {
fmt.Println("a")
B()
}

Import everything from a package

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

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")
}

Go build project with cmd and pkg layout - build error

I'm trying to build a Go project using the layout as described in Go Project Layout
I'm using go 1.9.2 on Ubuntu. My project layout is as follows
$GOPATH/src/github.com/ayubmalik/cleanprops
/cmd
/cleanprops
/main.go
/internal
/pkg
/readprops.go
The file cmd/cleanprops/main.go is referring to the cleanprops package i.e.
package main
import (
"fmt"
"github.com/ayubmalik/cleanprops"
)
func main() {
body := cleanprops.ReadProps("/tmp/hello.props")
fmt.Println("%s", body)
}
The contents of internal/pkg/readprops.go are:
package cleanprops
import (
"fmt"
"io/ioutil"
)
func check(e error) {
if e != nil {
panic(e)
}
}
func ReadProps(file string) string {
body, err := ioutil.ReadFile(file)
check(err)
fmt.Println(string(body))
return body
}
However when I build cmd/cleanprops/main.go, from inside directory $GOPATH/src/github.com/ayubmalik/cleanprops, using command:
go build cmd/cleanprops/main.go
I get the following error:
cmd/cleanprops/main.go:5:2: no Go files in /home/xyz/go/src/github.com/ayubmalik/cleanprops
What am I missing?
The document suggests this structure:
$GOPATH/src/github.com/ayubmalik/cleanprops
/cmd
/cleanprops
/main.go
/internal
/pkg
/cleanprops
/readprops.go
Import the package like this. The import path matches the directory structure below $GOPATH/src.
package main
import (
"fmt"
"github.com/ayubmalik/cleanprops/internal/pkg/cleanprops"
)
func main() {
body := cleanprops.ReadProps("/tmp/hello.props")
fmt.Println("%s", body)
}

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)
}

Resources