Home > front end >  no required module provides package github.com/go-sql-driver/mysql:
no required module provides package github.com/go-sql-driver/mysql:

Time:10-06

i want to install mysql library in go. and i copy the code from golang Documentation and return error no required module provides package github.com/go-sql-driver/mysql: go.mod file not found in current directory or any parent directory; see 'go help modules' and i don't know what to do becouse i try to install by $ go get -u github.com/go-sql-driver/mysql in the terminal

but the library need Requirements

Requirements

Go 1.10 or higher. We aim to support the 3 latest versions of Go. MySQL (4.1 ), MariaDB, Percona Server, Google CloudSQL or Sphinx (2.2.3 )

i have the first Requirement but the second i don't know what is

and the Installation

Installation

Simple install the package to your $GOPATH with the go tool from shell:

$ go get -u github.com/go-sql-driver/mysql Make sure Git is installed on your machine and in your system's PATH.

and i done $ go get -u github.com/go-sql-driver/mysql but i don't know what is $GOPATH and 'Make sure Git is installed on your machine and in your system's PATH.'

and this is the code from the link https://golang.org/doc/tutorial/database-access

package main

import (
    "database/sql"
    "fmt"
    "log"
    "os"

    "github.com/go-sql-driver/mysql"
)

var db *sql.DB

type Album struct {
    ID     int64
    Title  string
    Artist string
    Price  float32
}

func main() {
    // Capture connection properties.
    cfg := mysql.Config{
        User:   os.Getenv("DBUSER"),
        Passwd: os.Getenv("DBPASS"),
        Net:    "tcp",
        Addr:   "127.0.0.1:3306",
        DBName: "recordings",
    }
    // Get a database handle.
    var err error
    db, err = sql.Open("mysql", cfg.FormatDSN())
    if err != nil {
        log.Fatal(err)
    }

    pingErr := db.Ping()
    if pingErr != nil {
        log.Fatal(pingErr)
    }
    fmt.Println("Connected!")

    albums, err := albumsByArtist("John Coltrane")
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("Albums found: %v\n", albums)

    // Hard-code ID 2 here to test the query.
    alb, err := albumByID(2)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("Album found: %v\n", alb)

    albID, err := addAlbum(Album{
        Title:  "The Modern Sound of Betty Carter",
        Artist: "Betty Carter",
        Price:  49.99,
    })
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("ID of added album: %v\n", albID)
}

// albumsByArtist queries for albums that have the specified artist name.
func albumsByArtist(name string) ([]Album, error) {
    // An albums slice to hold data from returned rows.
    var albums []Album

    rows, err := db.Query("SELECT * FROM album WHERE artist = ?", name)
    if err != nil {
        return nil, fmt.Errorf("albumsByArtist %q: %v", name, err)
    }
    defer rows.Close()
    // Loop through rows, using Scan to assign column data to struct fields.
    for rows.Next() {
        var alb Album
        if err := rows.Scan(&alb.ID, &alb.Title, &alb.Artist, &alb.Price); err != nil {
            return nil, fmt.Errorf("albumsByArtist %q: %v", name, err)
        }
        albums = append(albums, alb)
    }
    if err := rows.Err(); err != nil {
        return nil, fmt.Errorf("albumsByArtist %q: %v", name, err)
    }
    return albums, nil
}

// albumByID queries for the album with the specified ID.
func albumByID(id int64) (Album, error) {
    // An album to hold data from the returned row.
    var alb Album

    row := db.QueryRow("SELECT * FROM album WHERE id = ?", id)
    if err := row.Scan(&alb.ID, &alb.Title, &alb.Artist, &alb.Price); err != nil {
        if err == sql.ErrNoRows {
            return alb, fmt.Errorf("albumsById %d: no such album", id)
        }
        return alb, fmt.Errorf("albumsById %d: %v", id, err)
    }
    return alb, nil
}

// addAlbum adds the specified album to the database,
// returning the album ID of the new entry
func addAlbum(alb Album) (int64, error) {
    result, err := db.Exec("INSERT INTO album (title, artist, price) VALUES (?, ?, ?)", alb.Title, alb.Artist, alb.Price)
    if err != nil {
        return 0, fmt.Errorf("addAlbum: %v", err)
    }
    id, err := result.LastInsertId()
    if err != nil {
        return 0, fmt.Errorf("addAlbum: %v", err)
    }
    return id, nil
}

CodePudding user response:

Just checking if you're new to Go because I had a very similar issue when I started. The Go import system is sorta weird but you'll get the hang of it. So you hopefully have all your go files in a src/ directory. So in the directory that contains the src/ directory, run go mod init which will create a go.mod file which points your go path to where the directory is located.

CodePudding user response:

https://golang.org/doc/tutorial/database-access#create_folder

if you write code in file named example.go you should do this steps

Open a command prompt and change to your home directory.

1 On Linux or Mac:$ cd On Windows:C:\> cd %HOMEPATH%

2 $ mkdir data-access

3 you should to move your file that you named example.go to folder or directory that named data-access you can find it in home directory

4 $ cd data-access

5 $ go mod init example

and then run your code in this new directory that you do it and just to know you can change the name of the directory but don't change the structure or tree of the directory

  • Related