package sea import ( "os" "fmt" "errors" _ "github.com/go-sql-driver/mysql" "github.com/jmoiron/sqlx" ) // ============= SEA ============ type Sea struct { db *sqlx.DB } func Connect() (*Sea, error) { user := os.Getenv("SEA_USER") if user == "" { return nil, errors.New("Не установлена переменная SEA_USER") } password := os.Getenv("SEA_PASSWORD") if password == "" { return nil, errors.New("Не установлена переменная SEA_PASSWORD") } db, err := openDB(user, password) if err != nil { return nil, err } sea := Sea{db: db} return &sea, nil } func openDB(user, password string) (*sqlx.DB, error) { conn := fmt.Sprintf("%s:%s@(xigmanas:3306)/sea", user, password) db, err := sqlx.Open("mysql", conn) if err != nil { return nil, err } return db, nil } // ============= ALBUM ============ type Album struct { Seria string Cnt int Hash string } func (s *Sea) Albums() ([]Album, error) { cmd := "call getAlbums()" var albums []Album err := s.db.Select(&albums, cmd) if err != nil { return nil, err } return albums, nil }