Recap - Building a Utility
Part of the Logic & Flow section of Coddy's GO journey — lesson 57 of 68.
Challenge
EasyCreate a comprehensive stringutil package with string manipulation functions and demonstrate its usage in a main program. This challenge will test your ability to create a custom utility package, implement exported functions, and use your package from another program.
You will receive two inputs:
- A string containing function specifications in the format
"function1:description1,function2:description2"(e.g.,"Reverse:reverses a string,ToUpper:converts to uppercase,ToLower:converts to lowercase") - A string containing test strings separated by pipes in the format
"string1|string2|string3"(e.g.,"hello|Go Programming|WORLD")
Your task is to:
- Create a
stringutilpackage by declaringpackage stringutil - Parse the first input by splitting on commas to get individual function specifications
- For each function specification, split on colons to get the function name and description
- Implement the following exported functions in your
stringutilpackage:Reverse(s string) string- returns the string with characters in reverse orderToUpper(s string) string- returns the string converted to uppercaseToLower(s string) string- returns the string converted to lowercaseLength(s string) int- returns the length of the string
- In your main program, import the
stringutilpackage - Display the package header:
"=== STRINGUTIL PACKAGE DEMONSTRATION ===" - Display the available functions:
"Available functions in stringutil package:"- For each function from the first input:
"- [function_name]: [description]"
- Parse the second input by splitting on pipes to get individual test strings
- Display the testing header:
"Testing stringutil functions:" - For each test string, demonstrate all functions:
"Testing with: \"[test_string]\""" stringutil.Reverse: \"[reversed_result]\""" stringutil.ToUpper: \"[uppercase_result]\""" stringutil.ToLower: \"[lowercase_result]\""" stringutil.Length: [length_result]"
- Calculate and display statistics:
"=== STATISTICS ===""Total test strings processed: [number_of_test_strings]""Functions available in package: [number_of_functions_from_first_input]""Total function calls made: [total_function_calls]"
- Display the longest and shortest strings:
"Longest string: \"[longest_string]\" (length: [length])""Shortest string: \"[shortest_string]\" (length: [length])"
- Display the completion message:
"Package demonstration completed successfully"
Use the strings package to split input strings and manipulate text, and the fmt package for formatted output. For the Reverse function, you'll need to convert the string to a slice of runes, reverse the order, and convert back to a string. Remember that all exported functions must start with capital letters to be accessible from other packages. This challenge demonstrates how to create reusable utility packages and organize related functionality together.
Try it yourself
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
// TODO: Create your stringutil package functions here
// Since we can't use separate files, define the functions directly
// These would normally be in a separate package file
// func Reverse(s string) string {
// // Convert to runes, reverse, convert back to string
// }
// func ToUpper(s string) string {
// // Use strings.ToUpper
// }
// func ToLower(s string) string {
// // Use strings.ToLower
// }
// func Length(s string) int {
// // Return len(s)
// }
func main() {
// Read input using bufio.Scanner to handle spaces properly
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
functionSpecs := scanner.Text()
scanner.Scan()
testStrings := scanner.Text()
// Parse function specifications
funcList := strings.Split(functionSpecs, ",")
// Parse test strings
testList := strings.Split(testStrings, "|")
// TODO: Write your code below
// 1. Display package header
// 2. Display available functions from funcList (check array lengths)
// 3. Test all functions with each test string
// 4. Calculate and display statistics
// 5. Find and display longest/shortest strings (check if testList has elements)
// 6. Display completion message
// Remember to use your stringutil package functions like:
// Reverse(testString)
// ToUpper(testString)
// ToLower(testString)
// Length(testString)
}All lessons in Logic & Flow
1Advanced Control Flow
Switch with `fallthrough`Breaking from Nested LoopsContinuing a Specific LoopThe `goto` StatementRecap - Advanced Loop Control4Project: Simple Task List
Project SetupAdding a Task2Structs and Methods
Defining Methods on StructsValue ReceiversPointer ReceiversChoosing ReceiversMethods vs FunctionsRecap - Struct Behavior5Maps In-Depth
Maps of StructsPointers as Map ValuesTesting for Nil MapsComparing MapsRecap - Word Frequency Counter3Interfaces (The Basics)
What is an Interface?Defining an InterfaceImplementing an InterfaceUsing Interface TypesEmpty InterfaceType AssertionsType SwitchRecap - Shapes and Behaviors6Idiomatic Go: Sets
The Set Idiom in GoCreating a SetAdding to a SetChecking for MembershipRemoving from a SetIterating Over a SetRecap - Unique Usernames9Packages and Scope
What is a Package?Exported vs UnexportedCreating a Simple PackagePackage AliasingThe Blank Identifier `_`The `init` functionRecap - Building a Utility