GolangWebDev
GolangWebDev
35 0 0

Golang mod import local package

To import a local package in Go, you can use the import statement followed by the path to the package, which is the directory containing the package's Go source files. The path should be relative to the current directory or an absolute path.

Here is an example of how to import a local package called mypackage:

import "./mypackage"

If you have your Go workspace set up correctly and your package is in the src directory of your workspace, you can also use the following import statement:

import "mypackage"

You can then use the functions and types defined in the package by referring to them using the package name as a prefix. For example, if the package defines a function called Hello, you can call it using mypackage.Hello().

Note that the package name is not necessarily the same as the name of the directory containing the package's source files. The package name is specified using the package keyword at the top of the source files in the package.

You can read more about Go's package system and how to use local packages in the Effective Go documentation.

The main go.mod

module example.com/localmodexample

go 1.13

require (
   example.org/hello v0.0.0
   example.org/utils v0.0.0

)

replace (
   example.org/hello => ./hello
   example.org/utils => ./utils
)

example

replace github.com/yourname/package1 => /goapps/src/github.com/yourname/package1

require github.com/yourname/package1 v1.2.4

version must be of the form v1.2.3

using the go mod edit

$ go mod edit -replace github.com/you/bar=/Users/you/Projects/bar
0

See Also


Discussion

Login Topics