With the rapid increase in the amount of information on the Internet, data processing has become an important issue that enterprises and institutions need to face. The choice of language and database for processing data also has a great impact on solving problems such as iterative processing of data. Go language is an efficient and lightweight programming language, while MySQL is a commonly used open source relational database. This article will introduce how to use Go language and MySQL database for data iterative processing.
1. What is data iterative processing?
Iteration refers to accessing each element in the collection in a loop. Data iterative processing means that when operating on data, each element in the data collection is operated once, and the operation is repeated until a certain condition is met and stops.
2. Go language and MySQL database connection
Go language provides a built-in database/sql package that can be used to connect various SQL databases. When using this package, you need to use a third-party driver. Commonly used MySQL drivers include go-sql-driver/mysql and mysql-connector-go. Here we use go-sql-driver/mysql as the driver to connect to the MySQL database. Before connecting to the MySQL database, use the go get command to obtain the driver.
go get github.com/go-sql-driver/mysql
The MySQL connection is represented by a DSN (Data Source Name) string. The DSN string contains all the information needed to connect to the database, including the database address, port number, user name, password, database name and other information. Use the following code to connect to the MySQL database:
import ( "database/sql" "fmt" _ "github.com/go-sql-driver/mysql" ) func main() { db, err := sql.Open("mysql", "username:password@tcp(ip:port)/database?charset=utf8mb4") if err != nil { fmt.Println("连接数据库失败:", err) return } defer db.Close() }
Among them, username represents the user name you use to log in to the MySQL database, password represents the login password, ip and port represent the address and port number of the MySQL server, and database is the name of the database. The charset is utf8mb4, which means using MySQL's UTF-8 encoding. After using up the connection pool, you need to close the connection, so use defer db.Close().
3. Data iteration processing in Go language
Go language provides a variety of loop methods, including for loop, range loop, while loop, etc. Among them, the range loop can be used to traverse collection types such as arrays, slices, Maps, strings, and channels.
The following is a sample code for data iterative processing of slices and Maps:
package main import "fmt" func main() { // 遍历切片 slice := []int{1, 2, 3, 4, 5, 6} for index, value := range slice { fmt.Printf("索引:%d,值:%d ", index, value) } // 遍历Map myMap := map[string]int{"apple": 1, "banana": 2, "orange": 3} for key, value := range myMap { fmt.Printf("key:%s,value:%d ", key, value) } }
The output result is:
索引:0,值:1 索引:1,值:2 索引:2,值:3 索引:3,值:4 索引:4,值:5 索引:5,值:6 key:apple,value:1 key:banana,value:2 key:orange,value:3
4. Data iterative processing of MySQL database
When processing data in the MySQL database, you first need to query the database and store the query results in a result set. Then, the result set can be iteratively operated to process each piece of data.
The following is a sample code for querying data in the MySQL database and performing data iteration processing:
package main import ( "database/sql" "fmt" _ "github.com/go-sql-driver/mysql" ) func main() { db, err := sql.Open("mysql", "username:password@tcp(ip:port)/database?charset=utf8mb4") if err != nil { fmt.Println("连接数据库失败:", err) return } defer db.Close() rows, err := db.Query("SELECT id, name, age FROM user") if err != nil { fmt.Println("查询数据失败:", err) return } defer rows.Close() for rows.Next() { var id, age int var name string err = rows.Scan(&id, &name, &age) if err != nil { fmt.Println("数据读取失败:", err) return } fmt.Printf("id:%d,name:%s,age:%d ", id, name, age) } }
The output result is:
id:1,name:Tom,age:18 id:2,name:Jerry,age:19 id:3,name:Bob,age:20 id:4,name:Linda,age:21 id:5,name:Lucy,age:22
The above code queries the data named user table, and use rows.Next() to traverse the query results, continuously read the values corresponding to name and age in each row of data, and output them to the console.
Summary
This article introduces how to use Go language and MySQL database for data iterative processing. First, connect the MySQL database through the DSN string and use the built-in database/sql package for data query; then, traverse data collections such as slices and Maps through the range loop; finally, introduce how to use the database query results for the data in the MySQL database. Data is processed iteratively to process each piece of data. Through the introduction of this article, everyone can better use the Go language to process data.
The above is the detailed content of Go language and MySQL database: How to perform data iterative processing?. For more information, please follow other related articles on the PHP Chinese website!