// Package greeting provides functionality for generating greeting messages.// Following Go conventions: package comment starts with "Package packagename"packagegreetingimport("fmt""strings")// Generate creates a simple greeting message.// Function comments start with the function name.funcGenerate(namestring)string{ifname==""{name="World"}returnfmt.Sprintf("Hello, %s! Welcome to Go programming.",name)}// Personalized creates a personalized greeting with language preference.// This demonstrates error handling, a Go best practice.funcPersonalized(name,languagestring)(string,error){ifname==""{return"",fmt.Errorf("name cannot be empty")}name=strings.TrimSpace(name)language=strings.ToLower(strings.TrimSpace(language))vargreetingstringswitchlanguage{case"go","golang":greeting=fmt.Sprintf("Hello, %s! Ready to Go? 🚀",name)case"spanish":greeting=fmt.Sprintf("¡Hola, %s!",name)case"french":greeting=fmt.Sprintf("Bonjour, %s!",name)default:greeting=fmt.Sprintf("Hello, %s!",name)}returngreeting,nil}// Constants demonstrate Go naming conventionsconst(DefaultLanguage="english"MaxNameLength=50)// Exported variables use PascalCasevarSupportedLanguages=[]string{"english","spanish","french","go"}
Running the Application
go run main.go
Building the Application
go build -o hello-world-app
./hello-world-app
Testing
go test ./...
Key Go Best Practices Demonstrated:
✅ Proper module initialization with domain-based naming
✅ Package organization (internal/ for private packages)
✅ Exported vs unexported identifiers (PascalCase vs camelCase)
✅ Function documentation following Go conventions
✅ Error handling with multiple return values
✅ Clear, descriptive naming without underscores
✅ Separation of concerns (main.go stays minimal)
Top comments (0)
Subscribe
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Top comments (0)