MySQL database operation in go Language (ii)

Source: Internet
Author: User

Connection pool

The go language comes with a database connection pool, which can be used directly and conveniently.

func init() {    db, _ = sql.Open("mysql", "root:[email protected](127.0.0.1:3306)/betting?charset=utf8")    // 设置最大打开的连接数,默认值为0表示不限制。    db.SetMaxOpenConns(2000)    // 用于设置闲置的连接数。    db.SetMaxIdleConns(1000)    db.Ping()}
    • By setting the maximum number of connections, you can avoid too many connections errors that occur when you connect to MySQL with too high concurrency.
    • Set the number of idle connections when a connection is opened, it can be placed in the pool for the next use.
Call a stored procedure

When I use go to invoke a stored procedure, how to get the value in the Out parameter and never find the correct method, I only summarize the method using the in parameter.

Stored Procedures

CREATE DEFINER = ‘root‘@‘localhost‘ PROCEDURE `proc_test`(        IN `in_id` INTEGER    )    NOT DETERMINISTIC    CONTAINS SQL    SQL SECURITY DEFINER    COMMENT ‘‘BEGIN    SELECT log_ip FROM admin_log WHERE id=in_id;END;

Call in Go

func main() {    db, err := sql.Open("mysql", "root:[email protected](127.0.0.1:3306)/betting?charset=utf8")    checkError("连接数据库", err)    defer db.Close()    var outArg string    row := db.QueryRow("call proc_test(?)", 2000)    row.Scan(&outArg)    fmt.Println(outArg)}

Execution results

192.168.88.91

MySQL database operation in go Language (ii)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.