> ## 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.

# Creating and accessing a package

> Guide to creating a Go module and local package that implements a Caesar style Encrypt function shifting each rune by three and demonstrates importing and running it from main

In this lesson you'll learn how to create a Go module, add a local package, and import that package from `main`. We'll build a small module named `github.com/Priyanka-yadavv/cryptit` that demonstrates a simple Caesar-style encryption (shift each character's Unicode code point by 3). This exercise covers module initialization, package layout, exported identifiers, and running the program.

## What you'll build

* Module: `github.com/Priyanka-yadavv/cryptit`
* Packages:
  * `encrypt` — implements the encryption algorithm (`algorithm.go`)
  * `decrypt` — reserved for the decryption algorithm (to be added later)
* At the module root: `main.go` — imports and uses `encrypt`

The encryption is a simple Caesar-style shift: add 3 to each rune (Unicode code point). Examples:

* A -> D
* M -> P

Planned project layout:

```bash theme={null}
cryptit/
  encrypt/
    algorithm.go
  decrypt/
    algorithm.go
  main.go
```

File overview

| Path | Purpose |
| - | - |
| `encrypt/algorithm.go` | Exported `Encrypt` function (shifts runes by 3) |
| `decrypt/algorithm.go` | Placeholder for a future `Decrypt` function |
| `main.go` | Uses `encrypt.Encrypt` to encrypt a string |

## 1. Create the module

Create the project directory and initialize a Go module using your repository import path (omit the `https://` prefix):

```bash theme={null}
mkdir -p ~/workspace/cryptit
cd ~/workspace/cryptit
go mod init github.com/Priyanka-yadavv/cryptit
```

Example `go.mod` generated (truncated):

```text theme={null}
module github.com/Priyanka-yadavv/cryptit

go 1.19
```

## 2. Create the encrypt package

1. Add the `encrypt` directory and the `algorithm.go` file:

```bash theme={null}
mkdir encrypt
touch encrypt/algorithm.go
```

2. Implement an exported `Encrypt` function. Exported names in Go must begin with an uppercase letter so other packages can import and use them. The example below uses `strings.Builder` for efficient string concatenation while preserving the intended shift-by-3 behavior.

```go theme={null}
package encrypt

import "strings"

// Encrypt shifts each rune in the input string by 3 and returns the result.
func Encrypt(s string) string {
	var b strings.Builder
	for _, r := range s {
		b.WriteRune(r + 3)
	}
	return b.String()
}
```

This implementation preserves Unicode behavior by iterating over runes (not bytes) and is idiomatic for building strings efficiently.

## 3. Create `main.go` to use the package

At the module root, create `main.go` and import the local package by using the module path followed by the package folder name.

```go theme={null}
package main

import (
	"fmt"

	"github.com/Priyanka-yadavv/cryptit/encrypt"
)

func main() {
	fmt.Println(encrypt.Encrypt("Kodekloud"))
}
```

Note that the import path is the module path plus `/encrypt`, matching the directory structure.

## 4. Build and run

From the module root run:

```bash theme={null}
go run main.go
```

Expected output:

```text theme={null}
Nrghnorxg
```

<Callout icon="lightbulb" color="#1CB2FE">
  In Go, an identifier (function, type, variable, or constant) is exported from a package when its name begins with an uppercase letter. Exported identifiers are accessible to importing packages; unexported identifiers begin with a lowercase letter and are only visible inside the same package.
</Callout>

## Summary

* Initialized a module: `github.com/Priyanka-yadavv/cryptit`.
* Created an `encrypt` package with an exported `Encrypt` function that shifts each rune by 3.
* Added `main.go` at the module root to import and use `encrypt.Encrypt`.
* You can later add a `decrypt` package to reverse the transformation.

## Links and references

* [Go Modules (official docs)](https://go.dev/ref/mod)
* [Go Packages (official docs)](https://pkg.go.dev/golang.org/x/tools/cmd/godoc)
* [Strings Builder in Go](https://pkg.go.dev/strings#Builder)

<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/be4c6e74-4d82-485a-8689-fe96bab4611d" />
</CardGroup>


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