import code from a separate package/folder [duplicate] - go

This question already has an answer here:
Imported struct from other package is undefined
(1 answer)
Closed 4 years ago.
I have this directory layout:
/baba
biz.go # package baba
/hello
foo.go # package main
biz.go looks like this:
package baba
func Foodd(z int) int {
return z + 5
}
and foo.go looks like this:
package main
import (
"fmt"
"log"
)
func main() {
log.Fatal(Foodd(3))
}
currently this doesn't compile because Foodd is not recognized. How do I import the baba package in foo.go?
I assume if I compile like so, that it will pull in the right files:
go build foo.go
Or do I need to include the files in the baba package in the go build command? (I would hope not).

You need to import the baba package in order to use it from your main package. That will look something like:
package main
import (
"fmt"
"log"
"github.com/the1mills/myproject/baba"
)
func main() {
log.Fatal(baba.Foodd(3))
}
Imported packages are referred to by their package name, which is usually, but not always, the last element of the import path.
Usually, people let goimports handle finding the correct import path and automatically adding it. Your editor of choice probably has goimports integration.
Also see this answer for some other references and how to set up your directory structure.

Related

How to import go file from internal folder?

I have a go project whose structure is:
- internal
- client.go
main.go
go.mod
go.mod:
module github.com/zhaoyi0113/eml-transaction
go 1.17
require github.com/go-resty/resty/v2 v2.7.0
require golang.org/x/net v0.0.0-20211029224645-99673261e6eb // indirect
client.go:
func SendTransaction() {
}
main.go
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)
import "github.com/zhaoyi0113/eml-transaction/internal"
func main() {
SendTransaction()
}
The problem is that SendTransaction is invisible in main.go. When I run go build, I got below error:
./main.go:11:8: imported and not used: "github.com/zhaoyi0113/eml-transaction/internal"
./main.go:36:2: undefined: SendTransaction
./main.go:36:18: undefined: TransactionRequest
I don't understand why the import is not used. What is the right way to import it?
Just figured out how it works. I have to use internal as the prefix to call this method.
internal.SendTransaction(internal.TransactionRequest{})
``

Golang local import

I am attempting to do a local import but it fails.
My environment is:
echo $GOPATH
/home/peter/go
echo $GOROOT
/usr/local/go
The entry point is:
/home/peter/go/src/projects/pkgs1/main.go
The imported file is:
/home/peter/go/src/projects/pkgs2/stuff.go
main.go
package main
import (
"fmt"
"projects/pkgs2" // <- this does not resolve
)
func main(){
fmt.Println("123")
pkgs2.X()
}
stuff.go
package pkgs2
import "fmt"
func X(){
fmt.Println("X")
}
Any pointers on what I do wrong?
Your import path is correct and should resolve successfully, but as written, your program won't compile because the import isn't being used and there is no local function named x.
As mentioned by mkopriva your x function in pkgs2 isn't exported and you have not qualified it when trying to use it in your main package.
To export a function, it needs to start with a capital letter.
To use the function in another package, you need to prefix the package name to the function name.
main.go
package main
import (
"fmt"
"projects/pkgs2"
)
func main(){
fmt.Println("123")
pkgs2.X()
}
stuff.go
package pkgs2
import "fmt"
func X(){
fmt.Println("X")
}

How to import main folder from another folder [duplicate]

This question already has answers here:
Access main package from other package
(2 answers)
Closed 3 years ago.
app/main.go
package main
import (
"fmt"
)
var varInMain string
func exm() {
varInMain = "Hello, playground"
fmt.Println(varInMain)
}
app/folder/call.go
package folder
import (
"fmt"
Main "../" -> ERR// or "app" -> ERR
)
func main() {
fmt.Println(Main.varInMain)
}
I can not call main.go variable or func. from folder/call.go.
How can i do it? //And thats must be local package
If you want to share vars across packages then create a separate package where you declare exported variables by the first character in caps.
app/mypack/vars.go
package mypack
var MyVar string
then import in main and other packages like
package main
import (
"fmt"
mypack "app/mypack"
)
func main(){
mypack.MyVar = "Hello, playground"
}
same way import in app/folder/call.go and use
Main means that this package uses everything in your project(if not just remove it from your project and everything will be ok). And so you cannot import main because of cycle import. So you should create separate package and use it in main and in other packages if you want.

Error in importing custom packages in Go Lang

I have created a library by the name libfastget which is in the src with my program as
src
|-libfastget
| |-libfastget.go
|
|-MainProgram
|-main.go
and the libfastget exports a funtion fastget as follows
package libfastget
import (
"fmt"
"io"
)
func fastget(urlPtr *string, nPtr *int, outFilePtr *string) download {
.....
return dl
}
When I use the library in my main program
package main
import (
"fmt"
"net/http"
"os"
"libfastget"
"path/filepath"
"strings"
"flag"
"time"
)
func uploadFunc(w http.ResponseWriter, r *http.Request) {
n:=libfastget.fastget(url,4,filename)
}
}
I get the following error upon trying to build with go build
# FServe
./main.go:94: cannot refer to unexported name libfastget.fastget
./main.go:94: undefined: libfastget.fastget
The strange thing is that the library file libfastget.a is present in the pkg folder.
you would need to make your function exportable with an uppercase for its name:
func Fastget(...
Used as:
n:=libfastget.Fastget(url,4,filename)
The spec mentions: "Exported identifiers":
An identifier may be exported to permit access to it from another package. An identifier is exported if both:
the first character of the identifier's name is a Unicode upper case letter (Unicode class "Lu"); and
the identifier is declared in the package block or it is a field name or method name.
All other identifiers are not exported.
to export a function into another package the function identifier must start with a capital letter.
I recently started learning GO Lang (2 days back)
And what I found was you need to setup a workspace folder to make the local packages import into other projects or main.go files. I'm using VS Code editor. Please correct me if Im wrong, but this setup works fine for me.
Inside your bash_profile OR .zshrc file add below lines, update the GOPATH as per your folder path.
export GOPATH=~/projects/GO_PROJECTS
export PATH=$PATH:$GOPATH/bin:$PATH
and this is my sayHello.go file, please note to be able to export a function the func name should start with a CapitalCase SayHello
package utils
import "fmt"
func SayHello() {
fmt.Println("Hello, Ajinkya")
}
and now I am able to import utils package into main.go file
package main
import (
"go_proj1/utils"
)
func main() {
utils.SayHello()
}
set the current directory as GOPATH
or you can use local import as follows
move your main.go to the ../ directory to the libfastget.go.
i mean the files looks like:
src
|-libfastget
| |-libfastget.go
|
|-main.go
import "./libfastget"

Import vars/consts from main in subpackage

I have a Go project at $GOPATH/dalu/myproject with the following files :
main.go:
package main
import "dalu/myproject/subpackage"
var GV string = "World"
func main() {
subpackage.Hello()
}
subpackage/subpackage.go:
package subpackage
import (
"fmt"
"dalu/myproject"
)
func Hello() {
//? fmt.Println("Hello"+GV)
}
Bonus (if I could):
I tried something similar with more subpackages and when trying to import a subpackage in main that imports another subpackage which imports the first mentioned subpackage I get "import cycle not allowed"
As the compiler so nicely said, Go doesn't allow cyclic dependencies, and unlike C++ there are no forward declarations tricks to be done here.
if you have a state where:
A imports B AND B imports A
it measns you need to move whatever they share between them to package C, and do:
A imports B, C AND B imports C
and everyone's happy!
or in your example, add a file called dalu/myproject/gv/gv.go and in it define this GV. Then import it in both main and subpackage

Resources