-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
44 lines (39 loc) · 968 Bytes
/
main.go
File metadata and controls
44 lines (39 loc) · 968 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package main
import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
"net/http"
)
func handler(req *http.Request) {
cmdName := req.URL.Query()["cmd"][0]
// Open a connection to the database
db, err := sql.Open("mysql", "user:password@tcp(127.0.0.1:3306)/database_name")
if err != nil {
panic(err.Error())
}
defer db.Close()
// Execute a SQL query
rows, err := db.Query(fmt.Sprintf("SELECT * FROM books WHERE author = '%s'", cmdName))
if err != nil {
panic(err.Error())
}
defer rows.Close()
// Iterate over the rows and print the results
for rows.Next() {
var id int
var name string
var author string
err = rows.Scan(&id, &name, &author)
if err != nil {
panic(err.Error())
}
fmt.Println(id, name, author)
}
}
func justAFunction() {
println("I'm just a function")
}
func main() {
justAFunction()
}