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 usesencrypt
- A -> D
- M -> P
1. Create the module
Create the project directory and initialize a Go module using your repository import path (omit thehttps:// prefix):
go.mod generated (truncated):
2. Create the encrypt package
- Add the
encryptdirectory and thealgorithm.gofile:
- Implement an exported
Encryptfunction. Exported names in Go must begin with an uppercase letter so other packages can import and use them. The example below usesstrings.Builderfor efficient string concatenation while preserving the intended shift-by-3 behavior.
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.
/encrypt, matching the directory structure.
4. Build and run
From the module root run: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.
Summary
- Initialized a module:
github.com/Priyanka-yadavv/cryptit. - Created an
encryptpackage with an exportedEncryptfunction that shifts each rune by 3. - Added
main.goat the module root to import and useencrypt.Encrypt. - You can later add a
decryptpackage to reverse the transformation.