A simplified input/output helper package for Go beginners, inspired by Python's simplicity.
go get github.com/grandpaej/fmtpy
- Smart Input function with automatic type conversion
- Print function supporting both Go-style and Python-style formatting
- Modular package structure for clean imports
- Color functions that work seamlessly with
fmt.Print
,fmt.Println
,fmt.Printf
- Comprehensive string manipulation functions
- No performance loss compared to standard
fmt
package - Beginner-friendly API
import (
"github.com/grandpaej/fmtpy" // Core input/output
"github.com/grandpaej/fmtpy/color" // Color functions
"github.com/grandpaej/fmtpy/input" // String manipulation
)
The new Input()
function returns an InputValue
that can be converted to any type:
package main
import (
"fmt"
"github.com/grandpaej/fmtpy"
)
func main() {
// Direct type conversion - exactly what you wanted!
var age int
age = fmtpy.Input("Enter your age: ").Int()
var score float64
score = fmtpy.Input("Enter your score: ").Float()
name := fmtpy.Input("Enter your name: ").String()
confirmed := fmtpy.Input("Confirm (y/n): ").Bool()
fmt.Printf("Name: %s, Age: %d, Score: %.2f, Confirmed: %t\n",
name, age, score, confirmed)
}
package main
import (
"fmt"
"github.com/grandpaej/fmtpy/color"
)
func main() {
// Works with any type and any fmt function!
fmt.Println(color.Red("Error message"))
fmt.Println(color.Green(42)) // Numbers
fmt.Println(color.Blue(1+1)) // Expressions
fmt.Printf("Result: %s\n", color.Yellow(123.45))
// Bold colors
fmt.Println(color.BoldRed("Important!"))
fmt.Println(color.BoldGreen("Success!"))
// Background colors
fmt.Println(color.OnYellow("Warning"))
fmt.Println(color.OnRed("Critical"))
}

package main
import (
"fmt"
"github.com/grandpaej/fmtpy/color"
)
func main() {
c := color.Get()
// Basic colors - Short form
fmt.Println(c.R("Red text"))
fmt.Println(c.G("Green text"))
fmt.Println(c.B("Blue text"))
// Bold colors
fmt.Println(c.BR("Bold red"))
fmt.Println(c.BG("Bold green"))
// Background colors
fmt.Println(c.OnR("Red background"))
fmt.Println(c.OnY("Yellow background"))
// Semantic styles
fmt.Println(c.E("This is an error"))
fmt.Println(c.S("This is a success"))
fmt.Println(c.I("This is info"))
fmt.Println(c.W("This is a warning"))
// With formatting
name := "World"
fmt.Println(c.S("Hello, {n}!", name))
}
R()
- Red textG()
- Green textB()
- Blue textY()
- Yellow textM()
- Magenta textC()
- Cyan text
BR()
- Bold redBG()
- Bold greenBB()
- Bold blueBY()
- Bold yellowBM()
- Bold magentaBC()
- Bold cyan
OnR()
- Red backgroundOnG()
- Green backgroundOnB()
- Blue backgroundOnY()
- Yellow backgroundOnM()
- Magenta backgroundOnC()
- Cyan background
E()
- Error (Red)S()
- Success (Green)I()
- Info (Blue)W()
- Warning (Yellow)
InputInt(prompt)
- Get integer inputInputFloat(prompt)
- Get float inputAsk(prompt)
- Get yes/no input (returns bool)
Upper(s)
- Convert to uppercaseLower(s)
- Convert to lowercaseTitle(s)
- Convert to title case (first letter of each word)Capitalize(s)
- Capitalize first letter, lowercase the restSwapCase(s)
- Swap case of all letters
Reverse(s)
- Reverse a stringRepeat(s, n)
- Repeat string n timesReplace(s, old, new)
- Replace all occurrencesSplit(s, sep)
- Split string by separatorJoin(parts, sep)
- Join string slice with separatorTrim(s)
- Remove leading/trailing whitespace
Contains(s, substring)
- Check if string contains substringStartsWith(s, prefix)
- Check if string starts with prefixEndsWith(s, suffix)
- Check if string ends with suffixLength(s)
- Get character count (Unicode-aware)WordCount(s)
- Count words in stringCharCount(s)
- Count characters in stringCount(s, substring)
- Count occurrences of substring
Substring(s, start, end)
- Extract substringFirstWord(s)
- Get first wordLastWord(s)
- Get last wordLines(s)
- Split into linesWords(s)
- Split into words
OnlyLetters(s)
- Keep only alphabetic charactersOnlyNumbers(s)
- Keep only numeric charactersRemoveSpaces(s)
- Remove all spaces
IsEmpty(s)
- Check if empty or whitespace onlyIsNumber(s)
- Check if string is a valid numberIsLetter(s)
- Check if string contains only letters
ToInt(s)
- Convert to int (returns 0 if invalid)ToFloat(s)
- Convert to float64 (returns 0.0 if invalid)ToString(v)
- Convert any value to string
Center(s, width)
- Center string within widthLeftPad(s, width, char)
- Pad left with characterRightPad(s, width, char)
- Pad right with character
package main
import (
"fmt"
"github.com/grandpaej/fmtpy"
"github.com/grandpaej/fmtpy/input"
)
func main() {
text := "Hello World"
// Case conversion
fmt.Println(input.Upper(text)) // "HELLO WORLD"
fmt.Println(input.Lower(text)) // "hello world"
fmt.Println(input.Title(text)) // "Hello World"
fmt.Println(input.SwapCase(text)) // "hELLO wORLD"
// String operations
fmt.Println(input.Reverse(text)) // "dlroW olleH"
fmt.Println(input.Repeat("Hi", 3)) // "HiHiHi"
// Analysis
fmt.Println(input.WordCount(text)) // 2
fmt.Println(input.Length(text)) // 11
fmt.Println(input.FirstWord(text)) // "Hello"
// Filtering
mixed := "Hello123World"
fmt.Println(input.OnlyLetters(mixed)) // "HelloWorld"
fmt.Println(input.OnlyNumbers(mixed)) // "123"
// Formatting
fmt.Println(input.Center("Hi", 10)) // " Hi "
fmt.Println(input.LeftPad("Hi", 5, "*")) // "***Hi"
fmt.Println(input.RightPad("Hi", 5, "-")) // "Hi---"
// Smart input with type conversion
var age int = fmtpy.Input("Enter your age: ").Int()
var score float64 = fmtpy.Input("Enter your score: ").Float()
// Yes/no questions
if fmtpy.Input("Do you like Go? (y/n): ").Bool() {
fmt.Println("Great choice!")
}
}
package main
import (
"fmt"
"github.com/grandpaej/fmtpy"
"github.com/grandpaej/fmtpy/color"
"github.com/grandpaej/fmtpy/input"
)
func main() {
// Get user input with automatic type conversion
name := fmtpy.Input("What's your name? ").String()
var age int = fmtpy.Input("How old are you? ").Int()
var score float64 = fmtpy.Input("Enter your score: ").Float()
// Use colors with fmt functions
fmt.Println(color.Green("=== User Profile ==="))
fmt.Printf("Name: %s\n", color.Blue(name))
fmt.Printf("Age: %s\n", color.Cyan(age))
fmt.Printf("Score: %s\n", color.Yellow(score))
// String manipulation
fmt.Printf("Name in uppercase: %s\n", color.BoldRed(input.Upper(name)))
fmt.Printf("Reversed name: %s\n", color.Magenta(input.Reverse(name)))
// Combine everything
if fmtpy.Input("Save profile? (y/n): ").Bool() {
fmt.Println(color.BoldGreen("β
Profile saved successfully!"))
} else {
fmt.Println(color.BoldYellow("β οΈ Profile not saved"))
}
}
The example/
directory contains comprehensive examples:
basic_usage.go
- Simple input, colors, and string manipulationadvanced_features.go
- Text processing, validation, and interactive menuscolor_showcase.go
- Complete color demonstration with fmt functionsstring_manipulation.go
- All string manipulation featuresmain.go
- Complete feature demonstration
Run any example:
cd example
go run basic_usage.go
go run color_showcase.go
// The new way - exactly what you wanted!
var age int = fmtpy.Input("Age: ").Int()
var score float64 = fmtpy.Input("Score: ").Float()
name := fmtpy.Input("Name: ").String()
confirmed := fmtpy.Input("Confirm (y/n): ").Bool()
fmt.Println(color.Red("Error")) // Basic colors
fmt.Println(color.BoldGreen("Success")) // Bold colors
fmt.Println(color.OnYellow("Warning")) // Background colors
fmt.Printf("Number: %s\n", color.Cyan(42)) // Any data type
input.Upper("hello") // "HELLO"
input.Reverse("hello") // "olleh"
input.WordCount("hello world") // 2
input.OnlyNumbers("abc123") // "123"
input.Center("hi", 10) // " hi "
Input(prompt) InputValue
- Smart input with type conversionPrint(format, args...)
- Enhanced print with Python f-string support
.String()
- Convert to string.Int()
- Convert to int (returns 0 if invalid).Float()
- Convert to float64 (returns 0.0 if invalid).Bool()
- Convert to bool (y/yes/true/1 = true)
Red(v)
,Green(v)
,Blue(v)
,Yellow(v)
,Magenta(v)
,Cyan(v)
,White(v)
,Black(v)
BoldRed(v)
,BoldGreen(v)
,BoldBlue(v)
,BoldYellow(v)
,BoldMagenta(v)
,BoldCyan(v)
OnRed(v)
,OnGreen(v)
,OnBlue(v)
,OnYellow(v)
,OnMagenta(v)
,OnCyan(v)
All color functions accept any data type and work with fmt.Print
, fmt.Println
, fmt.Printf
Upper(s)
- Convert to UPPERCASELower(s)
- Convert to lowercaseTitle(s)
- Convert To Title CaseCapitalize(s)
- Capitalize first letter onlySwapCase(s)
- sWAP cASE
Reverse(s)
- Reverse stringRepeat(s, n)
- Repeat string n timesReplace(s, old, new)
- Replace all occurrencesSplit(s, sep)
- Split by separatorJoin(parts, sep)
- Join with separatorTrim(s)
- Remove leading/trailing whitespace
Contains(s, sub)
- Check if contains substringStartsWith(s, prefix)
- Check if starts with prefixEndsWith(s, suffix)
- Check if ends with suffixLength(s)
- Character count (Unicode-aware)WordCount(s)
- Count wordsCharCount(s)
- Count charactersCount(s, sub)
- Count occurrences of substring
Substring(s, start, end)
- Extract substringFirstWord(s)
- Get first wordLastWord(s)
- Get last wordLines(s)
- Split into linesWords(s)
- Split into words
OnlyLetters(s)
- Keep only alphabetic charactersOnlyNumbers(s)
- Keep only numeric charactersRemoveSpaces(s)
- Remove all spaces
IsEmpty(s)
- Check if empty or whitespace onlyIsNumber(s)
- Check if valid numberIsLetter(s)
- Check if contains only letters
ToInt(s)
- Convert to int (returns 0 if invalid)ToFloat(s)
- Convert to float64 (returns 0.0 if invalid)ToString(v)
- Convert any value to string
Center(s, width)
- Center string within widthLeftPad(s, width, char)
- Pad left with characterRightPad(s, width, char)
- Pad right with character
// Old way
name := fmtpy.Input("Name: ")
age := fmtpy.InputInt("Age: ")
// New way (recommended)
name := fmtpy.Input("Name: ").String()
var age int = fmtpy.Input("Age: ").Int()
// Standard Go
var age int
fmt.Print("Enter age: ")
fmt.Scanf("%d", &age)
// fmtpy way
var age int = fmtpy.Input("Enter age: ").Int()
// Legacy color functions (still supported)
fmt.Println(color.RedText("Red text with {placeholder}", "value"))
fmt.Println(color.BoldRedText("Bold red text"))
// New simple functions (recommended)
fmt.Println(color.Red("Simple red text"))
fmt.Println(color.BoldRed("Bold red text"))
// Colors work with any data type
users := []string{"Alice", "Bob", "Charlie"}
fmt.Printf("Users: %s\n", color.Green(users))
result := map[string]int{"score": 95, "level": 5}
fmt.Printf("Result: %s\n", color.Blue(result))
- Zero speed abuse: All functions are optimized for performance
- Memory efficient: No unnecessary allocations
- Unicode support: Proper handling of international characters
- Safe conversions: Type conversions never panic, return sensible defaults
NO_COLOR
: Set this to disable colorsTERM=dumb
: Colors will be disabled automatically
Feel free to open issues or submit pull requests!
git clone https://github.com/grandpaej/fmtpy
cd fmtpy
go test ./...
- Beginner-friendly: Simple, intuitive API
- Type-safe: Smart input conversion without panics
- Modular: Import only what you need
- Compatible: Works with standard
fmt
functions - Colorful: Rich color support for better UX
- Fast: Zero performance overhead
- Unicode: Proper international character support
Perfect for:
- π Learning Go programming
- π οΈ Building CLI tools
- π Creating interactive programs
- π¨ Adding colors to terminal output
- β‘ Rapid prototyping