Build a Modular Go Project

GolangGolangBeginner
Practice Now

Introduction

In this challenge, you'll demonstrate your understanding of Go Modules by creating a modular project that showcases package management skills for a small utility library. The challenge involves creating a utility package with a function that converts a string to uppercase, and a main package that imports and uses the utility package's function. You'll also need to initialize Go Modules for both packages and ensure the main package successfully runs and prints the converted string.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL go(("Golang")) -.-> go/FunctionsandControlFlowGroup(["Functions and Control Flow"]) go/FunctionsandControlFlowGroup -.-> go/functions("Functions") subgraph Lab Skills go/functions -.-> lab-435640{{"Build a Modular Go Project"}} end

Build a Modular Go Project

In this challenge, you'll demonstrate your understanding of Go Modules by creating a modular project that showcases package management skills for a small utility library. To simplify the process, we've provided a setup script that initializes the project structure and pre-fills the basic code framework. Your task is to complete the TODO sections in the code.

Tasks

  1. Navigate to ~/project/utility/helper.go and complete the TODO section:

    // TODO: Implement the ToUpperCase function using strings.ToUpper
  2. Navigate to ~/project/main/main.go and complete the TODO sections:

    // TODO: Call the utility.ToUpperCase function
    // TODO: Print the result using fmt.Println

Requirements

  • Complete the provided ~/project/utility/helper.go and ~/project/main/main.go files by filling in the TODO sections.
  • The utility package should define a function named ToUpperCase that converts a string to uppercase.
  • The main package should call the ToUpperCase function and print its result.

Examples

After completing the TODO sections, your project structure should look like this:

~/project/
├── utility/
│   ├── go.mod
│   └── helper.go
└── main/
    ├── go.mod
    └── main.go

When you run the main package, the output should be:

cd ~/project/main
go get utility
go run main.go
HELLO, WORLD

Hints

  • The strings.ToUpper function from the standard library can help you convert a string to uppercase.
  • Use go mod init to initialize Go Modules. (It's already done for you)
  • Use go get utility to import the local utility package in the main package.
✨ Check Solution and Practice

Summary

In summary, this challenge requires you to create a modular Go project that demonstrates your understanding of Go Modules and package management. You'll need to develop a utility package with a function to convert a string to uppercase, and a main package that imports and uses the utility package's function. The challenge also involves initializing Go Modules for both packages and ensuring the main package runs successfully and prints the converted string.