Este es el tercer artículo de la serie "Clean Code in Go". Código limpio: Funciones y manejo de errores en Go: del caos a la claridad [Parte 1] Código limpio en marcha (Parte 2): Estructuras, métodos y composición sobre la herencia Introducción: Interfaces - Go's Secret Weapon He visto a los equipos crear interfaces de 20 métodos que se vuelven imposibles de probar, burlarse o mantener.Entonces se preguntan por qué Go se siente mal. "Aceptar interfaces, devolver estructuras" - si solo has oído un idioma de Go, es probablemente este. pero ¿por qué es tan importante? y por qué las interfaces de método único son la norma en Go, no la excepción? Errores de interfaz comunes que he encontrado: - Interfaces con más de 10 métodos: ~45% de código Enterprise Go - Definición de interfaces en el sitio de implementación: ~70% de los paquetes - Devuelve interfaces en lugar de tipos de hormigón: ~55% de las funciones Uso de una interfaz vacía en cualquier lugar: ~30% de las APIs - interfaz de nil vs confusión de apuntador de nil: ~25% de errores sutiles Después de 8 años trabajando con Go y debugando innumerables problemas relacionados con la interfaz, puedo decir: el uso correcto de las interfaces es la diferencia entre el código que lucha contra el lenguaje y el código que fluye como el agua. Satisfacción de la interfaz: Duck Typing para adultos En Go, un tipo satisface una interfaz automáticamente, sin declaración explícita: // Interface defines a contract type Writer interface { Write([]byte) (int, error) } // File satisfies Writer automatically type File struct { path string } func (f *File) Write(data []byte) (int, error) { // implementation return len(data), nil } // Buffer also satisfies Writer type Buffer struct { data []byte } func (b *Buffer) Write(data []byte) (int, error) { b.data = append(b.data, data...) return len(data), nil } // Function accepts interface func SaveLog(w Writer, message string) error { _, err := w.Write([]byte(message)) return err } // Usage - works with any Writer file := &File{path: "/var/log/app.log"} buffer := &Buffer{} SaveLog(file, "Writing to file") // OK SaveLog(buffer, "Writing to buffer") // OK Las pequeñas interfaces: el poder de la simplicidad Regla del Método Único Mira la biblioteca estándar de Go: type Reader interface { Read([]byte) (int, error) } type Writer interface { Write([]byte) (int, error) } type Closer interface { Close() error } type Stringer interface { String() string } Un método – una interfaz. ¿Por qué? // BAD: large interface type Storage interface { Save(key string, data []byte) error Load(key string) ([]byte, error) Delete(key string) error List(prefix string) ([]string, error) Exists(key string) bool Size(key string) (int64, error) LastModified(key string) (time.Time, error) } // Problem: what if you only need Save/Load? // You'll have to implement ALL methods! // GOOD: small interfaces type Reader interface { Read(key string) ([]byte, error) } type Writer interface { Write(key string, data []byte) error } type Deleter interface { Delete(key string) error } // Interface composition type ReadWriter interface { Reader Writer } type Storage interface { ReadWriter Deleter } // Now functions can require only what they need func BackupData(r Reader, keys []string) error { for _, key := range keys { data, err := r.Read(key) if err != nil { return fmt.Errorf("read %s: %w", key, err) } // backup process } return nil } // Function requires minimum - only Reader, not entire Storage Principio de segregación en acción // Instead of one monstrous interface type HTTPClient interface { Get(url string) (*Response, error) Post(url string, body []byte) (*Response, error) Put(url string, body []byte) (*Response, error) Delete(url string) (*Response, error) Head(url string) (*Response, error) Options(url string) (*Response, error) Patch(url string, body []byte) (*Response, error) } // Create focused interfaces type Getter interface { Get(url string) (*Response, error) } type Poster interface { Post(url string, body []byte) (*Response, error) } // Function requires only what it uses func FetchUser(g Getter, userID string) (*User, error) { resp, err := g.Get("/users/" + userID) if err != nil { return nil, fmt.Errorf("fetch user %s: %w", userID, err) } // parse response return parseUser(resp) } // Testing becomes easier type mockGetter struct { response *Response err error } func (m mockGetter) Get(url string) (*Response, error) { return m.response, m.err } // Only need to mock one method, not entire HTTPClient! Aceptar interfaces, devolver estructuras ¿Por qué esto importa // BAD: returning interface func NewLogger() Logger { // Logger is interface return &FileLogger{ file: os.Stdout, } } // Problems: // 1. Hides actual type // 2. Loses access to type-specific methods // 3. Complicates debugging // GOOD: return concrete type func NewLogger() *FileLogger { // concrete type return &FileLogger{ file: os.Stdout, } } // But ACCEPT interface func ProcessData(logger Logger, data []byte) error { logger.Log("Processing started") // processing logger.Log("Processing completed") return nil } Ejemplo práctico // Repository returns concrete types type UserRepository struct { db *sql.DB } func NewUserRepository(db *sql.DB) *UserRepository { return &UserRepository{db: db} } func (r *UserRepository) FindByID(id string) (*User, error) { // SQL query return &User{}, nil } func (r *UserRepository) Save(user *User) error { // SQL query return nil } // Service accepts interfaces type UserFinder interface { FindByID(id string) (*User, error) } type UserSaver interface { Save(user *User) error } type UserService struct { finder UserFinder saver UserSaver } func NewUserService(finder UserFinder, saver UserSaver) *UserService { return &UserService{ finder: finder, saver: saver, } } // Easy to test - can substitute mocks type mockFinder struct { user *User err error } func (m mockFinder) FindByID(id string) (*User, error) { return m.user, m.err } func TestUserService(t *testing.T) { mock := mockFinder{ user: &User{Name: "Test"}, } service := NewUserService(mock, nil) // test with mock } Interfaz Composición Interfaces de Embalaje // Base interfaces type Reader interface { Read([]byte) (int, error) } type Writer interface { Write([]byte) (int, error) } type Closer interface { Close() error } // Composition through embedding type ReadWriter interface { Reader Writer } type ReadWriteCloser interface { Reader Writer Closer } // Or more explicitly type ReadWriteCloser interface { Read([]byte) (int, error) Write([]byte) (int, error) Close() error } Aserciones de tipo y tipos de interruptores // Type assertion - check concrete type func ProcessWriter(w io.Writer) { // Check if Writer also supports Closer if closer, ok := w.(io.Closer); ok { defer closer.Close() } // Check for buffering if buffered, ok := w.(*bufio.Writer); ok { defer buffered.Flush() } w.Write([]byte("data")) } // Type switch - handle different types func Describe(i interface{}) string { switch v := i.(type) { case string: return fmt.Sprintf("String of length %d", len(v)) case int: return fmt.Sprintf("Integer: %d", v) case fmt.Stringer: return fmt.Sprintf("Stringer: %s", v.String()) case error: return fmt.Sprintf("Error: %v", v) default: return fmt.Sprintf("Unknown type: %T", v) } } Archivo de la etiqueta: The Gotchas // WARNING: classic mistake type MyError struct { msg string } func (e *MyError) Error() string { return e.msg } func doSomething() error { var err *MyError = nil // some logic return err // RETURNING nil pointer } func main() { err := doSomething() if err != nil { // TRUE! nil pointer != nil interface fmt.Println("Got error:", err) } } // CORRECT: explicitly return nil func doSomething() error { var err *MyError = nil // some logic if err == nil { return nil // return nil interface } return err } Comprobar el Nilo // Safe nil check for interface func IsNil(i interface{}) bool { if i == nil { return true } // Check if value inside interface is nil value := reflect.ValueOf(i) switch value.Kind() { case reflect.Ptr, reflect.Map, reflect.Slice, reflect.Chan, reflect.Func: return value.IsNil() } return false } Ejemplos reales de la biblioteca estándar io.Reader/Writer - Fundación de todo // Copy between any Reader and Writer func Copy(dst io.Writer, src io.Reader) (int64, error) // Works with files file1, _ := os.Open("input.txt") file2, _ := os.Create("output.txt") io.Copy(file2, file1) // Works with network conn, _ := net.Dial("tcp", "example.com:80") io.Copy(conn, strings.NewReader("GET / HTTP/1.0\r\n\r\n")) // Works with buffers var buf bytes.Buffer io.Copy(&buf, file1) http.Handler — Web Server in One Method type Handler interface { ServeHTTP(ResponseWriter, *Request) } // Any type with ServeHTTP method can be a handler type MyAPI struct { db Database } func (api MyAPI) ServeHTTP(w http.ResponseWriter, r *http.Request) { switch r.URL.Path { case "/users": api.handleUsers(w, r) case "/posts": api.handlePosts(w, r) default: http.NotFound(w, r) } } // HandlerFunc - adapter for regular functions type HandlerFunc func(ResponseWriter, *Request) func (f HandlerFunc) ServeHTTP(w ResponseWriter, r *Request) { f(w, r) // call the function } // Now regular function can be a handler! http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hello, World!") }) Patterns y antipatterns Modelo: Implementación de la interfaz condicional // Optional interfaces for extending functionality type Optimizer interface { Optimize() error } func ProcessData(w io.Writer, data []byte) error { // Basic functionality if _, err := w.Write(data); err != nil { return err } // Optional optimization if optimizer, ok := w.(Optimizer); ok { return optimizer.Optimize() } return nil } Anti-Pattern: Interfaces demasiado genéricas // BAD: interface{} everywhere func Process(data interface{}) interface{} { // type assertions everywhere switch v := data.(type) { case string: return len(v) case []byte: return len(v) default: return nil } } // GOOD: specific interfaces type Sized interface { Size() int } func Process(s Sized) int { return s.Size() } Tipos prácticos Define las interfaces del lado del consumidor, no la implementación Prefiere pequeñas interfaces a grandes Uso del embedding para la composición de la interfaz No devuelva interfaces sin necesidad Interfaz de NIL vs Pointer de NIL Utilice las afirmaciones de tipo con cuidado interfaz{} es un último recurso, no una primera Lista de control de interfaz - Interfaz tiene 1-3 métodos máximo - Interfaz definida cerca del uso - Las funciones aceptan interfaces, no tipos concretos - Las funciones devuelven tipos concretos, no interfaces No hay métodos no utilizados en las interfaces - Las afirmaciones de tipo manejan ambos casos (ok/no ok) - La interfaz{} se utiliza sólo cuando sea necesario Conclusión Las interfaces son la cola que mantiene los programas de Go juntos. Permiten el código flexible, probable y mantenible sin jerarquías de herencia complejas. Recuerde: en Go, las interfaces son implícitas, pequeñas y compostables. En el siguiente artículo, discutiremos paquetes y dependencias: cómo organizar el código para que el gráfico de importación sea plano y las dependencias sean unidireccionales. ¿Cuál es tu toma en el diseño de la interfaz? ¿Cuánto pequeño es demasiado pequeño? ¿Cómo decides cuándo crear una interfaz en comparación con el uso de un tipo de hormigón? Comparte tu experiencia en los comentarios!