With the advent of the big data era, massive data has begun to be widely used in various aspects. Whether it is e-commerce, finance, medical care or artificial intelligence, a large amount of data needs to be processed. For this reason, data storage technology is changing. In this transformation process, relational database is still the most important data storage technology. Oracle database is one of the most important commercial relational databases on the market today. This article will introduce how to use Go language to connect to Oracle database.
go get gopkg.in/goracle.v2
package main import ( "database/sql" "fmt" "log" _ "gopkg.in/goracle.v2" ) func main() { db, err := sql.Open("goracle", "username/[email protected](ip:port)/database") if err != nil { log.Fatal(err) } defer db.Close() rows, err := db.Query("SELECT username FROM ALL_USERS") if err != nil { log.Fatal(err) } defer rows.Close() for rows.Next() { var username string if err := rows.Scan(&username); err != nil { log.Fatal(err) } fmt.Println(username) } if err := rows.Err(); err != nil { log.Fatal(err) } }
In the above program, we first import the following packages:
Next, a connection to the Oracle database will be opened. Here, we use the connection method of "username/[email protected](ip:port)/database", where username and password are the user's authentication information, ip and port are the information of the database server, database Is the database to connect to. Finally, we use the db.Query() method to get some data from the database.
The above is the detailed content of How to connect golang to oracle. For more information, please follow other related articles on the PHP Chinese website!