Build a Math Utility Package

GolangBeginner
Practice Now

Introduction

In this revised challenge, you will use an existing Go package (challengeproject/mathutil) that implements a Square() function. Your goal is to create a main.go file with basic placeholders for importing and calling Square(). After replacing the placeholder TODOs, running the program should print the squared result of a given integer (e.g., 25 if the integer is 5).

Import and Use the mathutil Package

A complete mathutil.go already exists, providing a public Square(x int) int function. You only need to edit the placeholder main.go to:

  1. Import the challengeproject/mathutil package.
  2. Call the Square() function.
  3. Print the result.

Tasks

  1. Open main.go in the ~/project directory.
  2. Replace the TODOs:
    • Add the grouped import for "challengeproject/mathutil".
    • Call mathutil.Square() with your choice of integer (e.g., 5).
    • Use fmt.Println() to print the result.

Requirements

  • The main.go file must import challengeproject/mathutil.
  • The function call must be mathutil.Square(5) (can't be another integer).
  • Print the result to stdout.

Examples

When you successfully complete the challenge and run:

go run main.go

You should see output similar to:

25

(This example assumes you pass the integer 5 to Square().)

Hints

  • Go uses the module path to locate the package. Make sure your import path matches the module name in go.mod.
  • The Square() function has been fully provided for you in mathutil.go.

Summary

This simplified challenge focuses on importing and using a pre-existing Go package function. By updating main.go with the correct imports and function calls, you will demonstrate your understanding of Go modules, imports, and function usage. After successful completion, you should see the correct squared result printed to your terminal.

✨ Check Solution and Practice