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

> Learn to create a Go module with packages for encryption and decryption, implementing a simple ASCII shifting encryption function.

In this lesson, you'll learn how to create a Go module and organize it into packages for encryption and decryption functionality. We will design a simple project where the encryption logic involves adding three to each character's ASCII code. This tutorial covers setting up the module, creating the encrypt package, and accessing the package in your main application.

The project structure is designed as follows:

```text theme={null}
cryptit
├── encrypt
│   └── algorithm.go
└── decrypt
    └── algorithm.go
```

For now, we will focus on building and testing the **encrypt** package.

***

## Step 1: Creating the Go Module

Start by navigating to your workspace directory and creating a new directory for your module:

```bash theme={null}
mkdir crypt8
cd crypt8
```

Initialize the Go module using the appropriate module path (usually matching your repository structure). For example:

```bash theme={null}
go mod init github.com/Priyanka-yadavv/cryptit
```

This command generates a `go.mod` file with content similar to:

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

<Callout icon="lightbulb" color="#1CB2FE">
  The `go.mod` file defines your module’s dependencies and version. Ensure that the module path is aligned with your repository.
</Callout>

***

## Step 2: Building the Encrypt Package

Next, create the directory for the **encrypt** package and add a new file named `algorithm.go`:

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

In `algorithm.go`, begin by declaring the package:

```go theme={null}
package encrypt
```

### Implementing the Encryption Function

We'll implement a function that processes an input string by adding three to the ASCII value of each character. The function will:

* Take a string as input.
* Iterate over every character.
* Shift the ASCII code by three.
* Append the modified character to an output string.
* Return the encrypted string.

Since this function will be used in another package, it must be exported. In Go, exported functions should have names that start with a capital letter. We'll name our function **Nimbus**.

Below is the complete implementation:

```go theme={null}
package encrypt

func Nimbus(str string) string {
	encryptedStr := ""
	for _, c := range str {
		asciiCode := int(c)
		character := string(asciiCode + 3)
		encryptedStr += character
	}
	return encryptedStr
}
```

This function accepts a string, shifts each character’s ASCII code by three, and returns the resultant encrypted string.

***

## Step 3: Setting Up the Main Application

Return to the root directory of your module to create the primary application file, `main.go`. This file will test the encryption function from the **encrypt** package.

Create `main.go` and add the following content:

```go theme={null}
package main

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

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

In the code above, we import the `encrypt` package using its module path. The function `encrypt.Nimbus("KodekLoud")` is called to process the string. The parameter is passed directly without the need for the `str:` label, and the uppercase N in Nimbus makes it accessible outside its package.

<Callout icon="lightbulb" color="#1CB2FE">
  Remember, functions that need to be accessible from other packages must start with an uppercase letter.
</Callout>

***

## Step 4: Running Your Go Application

With everything set up, compile and run the application from the module's root directory:

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

When you run the command, you should see the following output:

```bash theme={null}
Nrghnorxg
```

This output confirms that each character in `"KodekLoud"` has been shifted by three ASCII positions (e.g., K becomes N, O becomes R, etc.), which verifies that the encryption algorithm is functioning correctly.

***

## Conclusion

In this lesson, you learned how to:

* Create and initialize a Go module using `go mod init`.
* Organize module components into separate packages for encryption and decryption.
* Implement and export an encryption function by capitalizing its name.
* Utilize the exported function in a `main.go` file to run your application.

In upcoming lessons, we will develop the **decrypt** package to add complementary decryption functionality.

Happy coding!

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