> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Imports and exports

> Guide to Go exports by capitalization, importing packages within modules, and using go mod replace to point to local modules during development

This guide explains how Go handles exports (visibility) and imports, plus practical tips for working with local modules during development. You'll learn:

* How exports are determined in Go
* How to import packages inside the same module
* How to import a local module from a different module using `go mod` replace

## Exporting in Go

Go determines visibility by the identifier's first character—there is no explicit `export` keyword.

| Rule | Effect | Example |
| - | - | - |
| Starts with uppercase | Exported: visible outside the package | `Nimbus`, `MyFunc` |
| Starts with lowercase or underscore | Unexported: package-private | `nimbus`, `_helper` |

Example: an exported function in package `encrypt`

```go theme={null}
package encrypt

// Nimbus encrypts the input string by shifting each rune by +3.
func Nimbus(str string) string {
	var encryptedStr string
	for _, r := range str {
		encryptedStr += string(r + 3)
	}
	return encryptedStr
}
```

If you rename `Nimbus` to `nimbus`, it becomes unexported and cannot be accessed from another package.

Keep in mind:

<Callout icon="lightbulb" color="#1CB2FE">
  Anything you export becomes part of your package's public API. Document exported identifiers and maintain backward compatibility unless you intentionally make a major version change.
</Callout>

## Importing packages

To use exported constants, variables, types, or functions from another package, add an import for that package. Without the import, Go code outside the package cannot access its exported identifiers.

## Importing packages within the same module

Assume the module path is `github.com/Priyanka-yadavv/cryptit` and the `encrypt` package is in `./encrypt`. From `main` you can call `encrypt.Nimbus` like this:

```go theme={null}
package main

import (
	"fmt"
	"github.com/Priyanka-yadavv/cryptit/encrypt"
)

func main() {
	fmt.Println(encrypt.Nimbus("KodeKloud"))
}
```

## Adding a decrypt package

Create a `decrypt` package (for example `./decrypt/algorithm.go`) that reverses the encryption:

```go theme={null}
package decrypt

// Nimbus decrypts the input string by shifting each rune by -3.
func Nimbus(str string) string {
	var decrypted string
	for _, r := range str {
		decrypted += string(r - 3)
	}
	return decrypted
}
```

Use both packages from `main` (same module):

```go theme={null}
package main

import (
	"fmt"
	"github.com/Priyanka-yadavv/cryptit/decrypt"
	"github.com/Priyanka-yadavv/cryptit/encrypt"
)

func main() {
	encryptedStr := encrypt.Nimbus("KodeKloud")
	fmt.Println(encryptedStr)
	fmt.Println(decrypt.Nimbus(encryptedStr))
}
```

This prints the encrypted string followed by the decrypted original string.

## Importing a local module from a different module

If you have two separate modules locally (for example, `cryptit` and `learn`) and try to import `github.com/Priyanka-yadavv/cryptit/encrypt` from `learn` before publishing `cryptit` to a VCS, `go mod tidy` (or `go build`) will attempt to resolve the module from the network and may report that the module exists but does not contain the requested package. Example error (illustrative):

```text theme={null}
go: finding module for package github.com/Priyanka-yadavv/cryptit/encrypt
go: downloading github.com/Priyanka-yadavv/cryptit v0.0.0-20221114082254-ee77ba24d5d5
example.com/learn imports
    github.com/Priyanka-yadavv/cryptit/encrypt: module github.com/Priyanka-yadavv/cryptit@latest found (v0.0.0-20221114082254-ee77ba24d5d5), but does not contain package github.com/Priyanka-yadavv/cryptit/encrypt
```

## Using go.mod replace for local development

During local development you can direct the consumer module to use a local copy of your module using the `replace` directive in the consumer's `go.mod`. From the `learn` module directory run:

```bash theme={null}
# Replace the module path with the relative filesystem path to your local cryptit module
go mod edit -replace=github.com/Priyanka-yadavv/cryptit=../cryptit
```

Then tidy dependencies:

```bash theme={null}
go mod tidy
```

After running the replace command, `learn`'s `go.mod` will include an entry like:

```go theme={null}
module example.com/learn

go 1.19

require github.com/pborman/uuid v1.2.1

replace github.com/Priyanka-yadavv/cryptit => ../cryptit
```

Now `learn` can import `github.com/Priyanka-yadavv/cryptit/encrypt` and Go will use the local `../cryptit` directory. Example `main.go` in `learn`:

```go theme={null}
package main

import (
	"fmt"
	"github.com/Priyanka-yadavv/cryptit/encrypt"
	"github.com/pborman/uuid"
)

func main() {
	u, err := uuid.NewRandom()
	if err != nil {
		fmt.Println("failed to generate uuid:", err)
		return
	}
	fmt.Println(u)

	encryptedStr := encrypt.Nimbus("KodeKloud")
	fmt.Println(encryptedStr)
}
```

Running `go run main.go` should print a UUID and then the encrypted string, using your local `cryptit` module code.

## Notes about replace

* The `replace` directive substitutes a module path with another module path or a local directory. It works at the module level, not at the individual package path level, so point the right-hand side to the module root directory.
* Use `replace` only for development. When you publish the module to a VCS (for example, GitHub) and tag versions, consumers should import the published module and the `replace` directive is not necessary.

Summary table: module replace behaviors

| Scenario | Recommended action |
| - | - |
| Local development, consumer and library are separate modules | Use `go mod edit -replace=<module>=<local-path>` in consumer |
| Publishing library to VCS | Push & tag releases; remove `replace`, consumers use remote module |
| Mistaken package path in module | Ensure consumer imports `module/path/package` that actually exists in the module root |

## Links and references

* [Go Modules Reference](https://go.dev/ref/mod)
* [Go Blog: Using Go Modules](https://blog.golang.org/using-go-modules)
* [GitHub](https://github.com)

That covers Go's capitalization-based exports, importing packages inside the same module, and using `go mod replace` to work with local modules during development.

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/advanced-golang/module/6ce7d245-515e-428d-9672-915e49a8ebd6/lesson/f3efe8ac-b1e3-48ba-bb30-2cf16009f275" />
</CardGroup>


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.