2018-07-23 Golang Study Notes

Source: Internet
Author: User

1. Heartbeat Pack: The heartbeat packet is sent to continuously monitor and send the effect of reaching long connection

2. Handling connections to MySQL and SQLite

3.KCP go here to download kcp-go

4.protoc.exe go here to download protoc-3.6.0-win32.zip

5.bat Regular expression preceded by a REM indicating that the next regular match

@echo off

ECHO is building go code ...

REM protoc-i=./--go_out=. /pb/. \messageid.proto

Pause

Handling connections to MySQL and SQLite

Package Conntest

Import (

"Database/sql"

"FMT"

_ "Github.com/go-sql-driver/mysql"

_ "Github.com/mattn/go-sqlite3"

"OS"

"Testing"

)

Func testmysqlconn (t *testing. T) {

DB, err: = SQL. Open ("MySQL", "root:huhao123@tcp (127.0.0.1:3306)/game?charset=utf8")

If err! = Nil {

Fmt. PRINTLN (ERR)

Return

}

Defer db. Close ()

var result sql. Result

result, err = db. Exec ("INSERT into user_table (user_id, Name,password) VALUES (?,?,?)", 1234, "Huhaos", "huhao123")

If err! = Nil {

Fmt. PRINTLN (ERR)

Return

}

LastID, _: = result. Lastinsertid ()

Fmt. PRINTLN ("The ID of the newly inserted record is", LastID)

var row *sql. Row

row = db. Queryrow ("SELECT * from User_table")

var name, passwordstring

var user_idint

Err = row. Scan (&user_id, &password, &name)

If err! = Nil {

Fmt. PRINTLN (ERR)

Return

}

Fmt. Println (user_id, "\ T", name, "\ T", password)

result, err = db. Exec ("INSERT into user_table (User_id,name,password) VALUES (?,?,?)", 4444, "Black", "huhao123456")

var rows *sql. Rows

rows, err = db. Query ("SELECT * from User_table")

If err! = Nil {

Fmt. PRINTLN (ERR)

Return

}

For rows. Next () {

var name, passwordstring

var user_idint

Rows. Scan (&user_id, &name, &password)

Fmt. Println (user_id, "\ T", name, "\ T", password)

}

Rows. Close ()

Db. Exec ("TRUNCATE TABLE user")

}

Func testsqliteconn (t *testing. T) {

Dbconnector, err: = SQL. Open ("Sqlite3", "./... /db/sqllite.db ")

If err! = Nil | | Dbconnector = = Nil {

Fmt. Printf ("Connection Failed")

Os. Exit (1)

}

Dbconnector.setmaxopenconns (16)

Dbconnector.setmaxidleconns (8)

P: = dbconnector.ping ()

Fmt. Printf ("Connection succeeded%v", p)

SqlString: =string ("replace into user_table (User_id,name,password) VALUES (?,?,?)")

stmt, err__: = Dbconnector.prepare (SqlString)

Defer stmt. Close ()

If err__! = Nil {

Fmt. PRINTLN ("Create Err", err__)

Return

}

result, err: = stmt. Exec (

33334,

"Huhao",

"Huhao123",

)

If err! = Nil {

Fmt. PRINTLN ("Create err", err)

}

Effectrow, err: = result. Rowsaffected ()

If err! = Nil {

Fmt. PRINTLN ("Create err", err)

}

Fmt. PRINTLN ("Create Effect rows:", Effectrow)

}

Func testredisconn (t *testing. T) {

}

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.