Home >Backend Development >Golang >golang query mysql assignment

golang query mysql assignment

王林
王林Original
2023-05-14 19:12:08540browse

Golang is a programming language that supports concurrent programming, with an efficient garbage collection mechanism and fast compilation speed. When writing web applications using golang, you often need to interact with the MySQL database. This article will introduce how to use golang to query mysql and assign the results to variables.

First, you need to install golang’s MySQL driver. There are currently two commonly used MySQL drivers: go-sql-driver and mysql. In this article, we will use go-sql-driver.

To install go-sql-driver, you can use the following command:

go get -u github.com/go-sql-driver/mysql

After the installation is complete, import the mysql package in the code:

import (
    "database/sql"
    "fmt"
    _ "github.com/go-sql-driver/mysql"
)

Next, you need to establish a connection with the database connect. You can use the Open function to open a connection to the database and the Close function to close the connection. When opening a connection, you need to specify the database connection address, user name, password and database name.

db, err := sql.Open("mysql", "user:password@tcp(127.0.0.1:3306)/mydb")
if err != nil {
    panic(err.Error())
}
defer db.Close()

Next, you can use the Query function to query the database and assign the query results to variables. The Query function returns an object of type Rows that contains all rows of the query results. The Rows object provides multiple methods to traverse query results, such as Next, Scan, and Columns.

var name string
var age int
err = db.QueryRow("SELECT name, age FROM users WHERE id = ?", 1).Scan(&name, &age)
if err != nil {
    panic(err.Error())
}

In the above code, the QueryRow function is used to query the database. The QueryRow function returns a row of data, not all matching rows. The Scan function assigns each column value in the query result to the variables name and age just declared.

Finally, you can use the fmt package to output the assignment results to the console.

fmt.Printf("Name: %s
Age: %d
", name, age)

The complete code is as follows:

package main

import (
    "database/sql"
    "fmt"
    _ "github.com/go-sql-driver/mysql"
)

func main() {
    db, err := sql.Open("mysql", "user:password@tcp(127.0.0.1:3306)/mydb")
    if err != nil {
        panic(err.Error())
    }
    defer db.Close()

    var name string
    var age int
    err = db.QueryRow("SELECT name, age FROM users WHERE id = ?", 1).Scan(&name, &age)
    if err != nil {
        panic(err.Error())
    }

    fmt.Printf("Name: %s
Age: %d
", name, age)
}

The above is how to use golang to query mysql and assign the results to variables. By becoming proficient in this technology, developers can more easily handle database query operations and improve the efficiency and performance of web applications.

The above is the detailed content of golang query mysql assignment. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn