# go > Go idiomatic patterns - Author: PHAN ANH TUAN - Repository: TUAN130294/Universalkit - Version: 20251220090240 - Stars: 0 - Forks: 0 - Last Updated: 2026-02-07 - Source: https://github.com/TUAN130294/Universalkit - Web: https://mule.run/skillshub/@@TUAN130294/Universalkit~go:20251220090240 --- --- name: go description: Go idiomatic patterns --- # Go Skill ## Struct and Methods ```go type User struct { ID int64 Email string Name string } func (u *User) IsValid() error { if u.Email == "" { return errors.New("email is required") } return nil } ``` ## Error Handling ```go func GetUser(id int64) (*User, error) { user, err := db.Query("SELECT * FROM users WHERE id = ?", id) if err != nil { return nil, fmt.Errorf("failed to get user: %w", err) } return user, nil } // Usage user, err := GetUser(123) if err != nil { log.Printf("error: %v", err) return } ``` ## Interfaces ```go type UserRepository interface { GetByID(id int64) (*User, error) Create(user *User) error } type PostgresUserRepo struct { db *sql.DB } func (r *PostgresUserRepo) GetByID(id int64) (*User, error) { // implementation } ``` ## Best Practices - Return errors, don't panic - Use interfaces for abstraction - Accept interfaces, return structs - Use contexts for cancellation - Handle errors immediately - Use defer for cleanup