Can't find custom package - go

I'm tryng to create a custom package in go: i created folder project:
my_project
|_database
|_database.go
main.go
but when i try to import it gaves me this error: "could not import database (no package for import database)"
i try to run "go init" as written in some tutorial and created a "go.mod" file, then i run "go install" and works fine, but in the main.go it still not working.
The project structure is now this:
my_project
|_database
|_database.go
|_go.mod
|_go.sum
main.go
I work on windows with visual studio code
Here is how i import the package:
import (
"fmt"
...
"database"
)
function main() {

You need to do a few things.
Make sure your database.go is the following way:
it should contain a package name this case is database, comment the
function/s that need to be used outside this package, use func instead
of function. and comment code and capitalize function name so it can
be exported.
package database
import "fmt"
// Connect function to connect to my database
func Connect(){
fmt.Print("connected...")
}
your main.go should be the following.
use func instead of function, import database based on folder position
this case is ./ use the package name database. because this function
does not belong from current package main.
package main
import "./database"
func main() {
database.Connect()
}
This should be your folder structure.
my_project
|_database
|_database.go
main.go
go.mod
go.sum

Related

golang: How to manage packages in the local file-system

i am sure this has been asked many, many times: »How can i create and manage my go-packages locally without for example github ?«
I need to convert a couple of tools that were written in Perl to golang and i am fighting with golang's package management, simple example:
I wrote the main-package, containing the main function and a couple of other functions.
Now i want to move a group of functions into another package:
Created another package in the same directory and moved the group of
functions there
Built the package and the binary-file was created
Tried to import this package into the main package and miserably
failed. "PackageName", "./PackageName" "[fullpath]/PackageName" all
fails
For starters: How can i import a package from the local file-system?
Run go mod init <module_name> in the dir where you want to store all your code.
Example: go mod init auth-api
This will create a go.mod file in your root folder.
You can create a separate folder for each package.
Your dir structure looks like this.
package_1
-- file_1_1.go
-- file_1_2.go
package_2
-- file_2_1.go
-- file_2_2.go
-- file_2_3.go
main.go
go.mod
Here package_1 contains the file_1_1.go and file_1_2.go. Start these files with package package_1
Ex:
// file_1_1.go
package package_1
import "fmt"
func SayHelloFromPackage_1(){
fmt.Println("Hello from Package 1")
}
Now you can use that function in main.go:
// main.go
import "auth-api/package_1" // auth-api is our module name (go mod init auth-api)
func main() {
package_1.SayHelloFromPackage_1()
}

Golang Module problem--package xxx/xxxx is not in GOROOT

so here is what my directory is:
go
|-src
|-ppppppSample
|-newFolderOne
|-firstSample.go
|-hello.go
|-go.mod
and here is the content of hello.go
package main
import (
"fmt"
jjj "ppppppSample/newFolderOne"
)
func main() {
fmt.Println("start to test")
fmt.Println(jjj.FirstVVVV)
}
here is the content of firstSample.go
package newFolderOne
var FirstVVVV = "Im first SSSSSSSSSSSS"
and here is my go.mod's content
module mmmmmppppp
go 1.15
when giving it the cmd go run hello.go, the terminal came out with like this:
D:\Users\eien_zheng\go\src\ppppppSample>go run hello.go
hello.go:5:2: package ppppppSample/newFolderOne is not in GOROOT (C:\Go\src\ppppppSample\newFolderOne)
So here is my question:
(since I'm new for golang, I wish you guys could understand and tolerate some of my misunderstanding)
According to my understanding to Go module (maybe it's wrong), the Go module's function is gonna let some kind of online resource be downloaded to the directory GOPATH/pkg/mod instead of existing in GOROOT.
No matter which directory your project in, your project can still import those resource from GOPATH/pkg/mod if you init Go module.
But!!, in my understanding, it still can use package system to import package around project directory, in the meantime import online resource by Go module system.
How is that when I do (mod init) for hello.go, then it loses the (basic package import function) for this project?
|--src
|--sample
|--newFolder
|-firstSample.go (package xyz)
|--hello.go (package main import(xyz "sample/newFolder")
|--go mod (module sample go 1.15)
go mod should reference the root folder , here the root folder is |--sample
module sample
go v1.xx
inside hello.go;
package main
import ( xyz "sample/newFolder")
and make sure exported functins or variables use camelCase aka starts with BlockLetters.
Import packages within a module using the module's path:
package main
import (
"fmt"
jjj "mmmmmppppp/newFolderOne"
)
...
Run it on the Playground.

undefined main - testing code unable to acess main package

In the below code:
../folder1/some_test.go
package main_test
import "testing"
func TestF(t *testing.T) {
main.F()
}
../folder1/some_file.go
package main
func F() {
}
main.F() gives undefined main
$ go version
go version go1.14.3 linux/amd64
Renaming package name from main_test to main resolve the problem.
Why main_test package name is not allowed for testing code, in same folder?
You must import the package to use the package.
go.mod:
module example.app
main.go:
package main
func F() {}
func main() {}
main_test.go
package main_test
import (
"testing"
"example.app" // import the main package
)
func TestF(t *testing.T) {
main.F()
}
The code above assumes that main*.go are in the same directory as go.mod for package example.app. Replace example.app with the name of your module. Adjust the paths to match your application. For example, if the the main*.go files are in the directory cmd/example below the directory containing go.mod, use the import path example.app/cmd/example instead of example.app.
Note that a test can import a main package this way, but non-test code cannot.
The problem is that main_test is a different package from main.
To access functions in main you need to import main and access the functions like this: main.F()
Also, note f starts with lowercase and thus is not exported from the package main. To access it in main_test it needs to be exported (which can be done by changing it to start with a capital letter: F).
Alternatively, you can change the test file to be in package main.
Edit with a note: When importing main note that import paths are by directory name. Usually developers in Go put their packages in directories with the same name as the package (e.g. put main in a directory named main). In your case the package and directory names are different so the import will be import ".../folder1" not import ".../main". You'll still be able to use main.F() to access the function:
package main_test
import (
"testing"
"../folder1" // use the full path to folder1 from the root of your module
)
func TestF(t *testing.T) {
main.F()
}

How do I fix VS Code error: "Not able to determine import path of current package by using cwd" for Go project

I'm following tutorials and I think I may have missed something.
I have a Go project located at:
/Users/just_me/development/testing/golang/example_server
The contents are:
main.go
package main
import "fmt"
func main() {
fmt.Println("hi world")
}
I have a ~/go directory.
go env shows:
GOPATH="/Users/just_me/go"
GOROOT="/usr/local/Cellar/go/1.12.9/libexec"
I installed the suggested packages in VSCode.
When I save my main.go I get:
Not able to determine import path of current package by using cwd: /Users/just_me/development/testing/golang/example_server and Go workspace:
/Users/just_me/development/testing/golang/example_server>
How do I fix this?
Since your package is outside of $GOPATH, you may need to create a module file.
You'll need to init your go module using
go mod init your.import/path
Change the import path to whatever you like.
This way you set the import path explicitly, which might help fix it.
The resulting go.mod file looks like this:
module your.import/path
go 1.14 // Your go version
So if the go.mod file is in the same directory as a main.go file, you can now import submodules from it:
E.g. main.go:
package main
import (
"your.import/path/somepackage" // Import package from a subdirectory. This only works if `go.mod` has been created as above
)
func main() {
somepackage.SomeMethod()
}
And in somepackage/whatever.go:
package somepackage
import "fmt"
func SomeMethod() {
fmt.Println("Success!")
}
if you are using vs code, check if the go and code runner extensions are enabled, if enabled, try disabling and enabling again, and if not, install and enable, and download all requested packages.

How to write packages in go?

go is my first statically typed and compiled language so I am not sure about how to go about doing some things like writing packages...Should I write a package like...
package mypkg
import "fmt"
func mypkg {
fmt.Println("mypackage")
}
and then import this into the main package....install it and then test if it works?
Or should I write mypkg like this...
package main
import "fmt"
func mypkg() {
fmt.Println("mypackage")
}
func main() {
mypkg()
}
Should I do it this way to make sure that I can call go run mypkg and see if it works, then delete the main function and rename the package and then work on the rest of the library?
Or did I miss the point entirely and I should do something else?
Here are some basic steps to get you going if you want to use packages.
Create a folder called mypkg in the folder you're working in. In that folder create a mypkg.go file with the following code (note the capital letter in the function name. You need to do this so main.go can see it).
package mypkg
import "fmt"
func Mypkg {
fmt.Println("mypackage")
}
Now install the package just by typing go install while you're still in your mypkg directory. This will create a linkable object file you can use.
Now go back to your starting directory and create main.go with the following code:
package main
import (
"...(path to your mypkg folder)/mypkg"
"fmt"
)
func main() {
mypkg.Mypkg()
//optional
//fmt.Println("whatever")
}
Now run it by typing go run main.go. This will compile and run main.go that uses the previous mypkg package you created.

Resources