Answer the question
In order to leave comments, you need to log in
How to organize code into golang interfaces?
I have different models of equipment that do the same but in their own way, I want to organize the code so that when adding a handler for new equipment, I only make a package with the functions described in some main interface and add it to the enumeration somewhere. But only so that in other places the method call looks the same.
Something like this:
test123.go:
package test123
type Command interface {
Open()
Close()
}
package a123
import "fmt"
type A123struct struct {
hash string
iftrue bool
password string
}
func (variab *A123struct) Open() {
fmt.Println("open a123", variab.hash, variab.iftrue, variab.password)
}
func (variab *A123struct) Close() {
fmt.Println("close a123", variab.hash, variab.iftrue, variab.password)
}
package b123
import "fmt"
type B123struct struct {
id string
date bool
}
func (variab *B123struct) Open() {
fmt.Println("open b123", variab.id, variab.date)
}
func (variab *B123struct) Close() {
fmt.Println("close b123", variab.id, variab.date)
}
Answer the question
In order to leave comments, you need to log in
The interface will need to be specified as a parameter of functions that will be able to work with your interface.
For example.
func Process(device test123.Command) {
device.Open()
device.Close()
}
dev1 := a123.A123struct{параметры...}
Process(dev1)
dev2 := b123.B123struct{параметры...}
Process(dev2)
// или с перечислением
devices := []test123.Command{dev1, dev2}
for _, device := range devices {
Process(device)
}
package main
import (
"fmt"
"os"
)
// Logger интерфейс логгера.
type Logger interface {
Error(msg string)
}
// StdoutLogger реализация интерфейса Logger для вывода сообщений в консоль.
type StdoutLogger struct{}
// NewStdoutLogger конструктор (возвращаем структуру, не интерфейс)
func NewStdoutLogger() *StdoutLogger {
return &StdoutLogger{}
}
// Error добавляет в лог сообщение с уровнем error
func (l *StdoutLogger) Error(msg string) {
fmt.Printf("ERROR: %s\n", msg)
}
// FileLogger реализация интерфейса Logger для вывода сообщений в файл.
type FileLogger struct {
FileName string
Fh *os.File
}
// NewFileLogger конструктор.
func NewFileLogger(fileName string) *FileLogger {
logger := &FileLogger{
FileName: fileName,
}
fh, err := os.OpenFile(fileName, os.O_RDWR|os.O_CREATE, 0664)
if err != nil {
panic(fmt.Sprintf("FileLogger: can't open log file: %s: %s\n", fileName, err))
}
logger.Fh = fh
return logger
}
// Error добавляет в лог сообщение с уровнем error.
func (l *FileLogger) Error(msg string) {
l.Fh.WriteString(fmt.Sprintf("ERROR: %s\n", msg))
}
// ProcessData какая то функция, которая использует логгер и которую не должна беспокоить реализация логгера.
// Тут тип параметра должен быть интерфейс.
func ProcessData(logger Logger) {
logger.Error("Data process some error happened")
}
func main() {
var logger Logger
// если лог файл не указан - используем StdoutLogger, если указан - используем FileLogger
logFile := os.Getenv("LOG_FILE")
if logFile != `` {
logger = NewFileLogger(logFile)
} else {
logger = NewStdoutLogger()
}
ProcessData(logger)
}
go run main.go
you will see a message in the console: ERROR: Data process some error happened
LOG_FILE=test.log go run main.go
export LOG_FILE=test.log
go run main.go
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question