wufeng 发布的文章

仅以此记录自己学习Go语言的过程。今日开始每天做下相应的记录,方便自己复习,同时也可以让一些想入门的看看吧。开发环境安装就不再说了,这些内容网上太多了。
像其它很多语言一样,就从hello world开始吧。我是在macbook pro使用Visual Studio Code进行学习。与windows下的执行稍稍有些不同。之前一直使用的是php程序进行开发,程序中可能会将go与php的写法做一些对比。


- 阅读剩余部分 -

package main

import (
    "database/sql"
    "fmt"
    "log"
    "os"

    _ "github.com/mattn/go-sqlite3"
)

func main() {
    os.Remove("./products.db")
    db, err := sql.Open("sqlite3", "./products.db")
    if err != nil {
        log.Fatal(err)
    }
    sql := `create table t_products(id integer not null primary key, name text, price float)`
    _, err = db.Exec(sql)
    defer db.Close()
    if err != nil {
        log.Fatal(err)
        return
    }
    // 插入的事务开始
    tx, err := db.Begin()
    if err != nil {
        log.Fatal(err)
        return
    }
    stmt, err := tx.Prepare("insert into t_products(id, name, price) values(?,?,?)")
    if err != nil {
        log.Fatal(err)
        return
    }
    defer stmt.Close()
    for i := 0; i < 10; i++ {
        _, err = stmt.Exec(i+1, fmt.Sprintf("产品%d", i+1), float64(i+1)*54.8)
        if err != nil {
            log.Fatal(err)
            return
        }
    }
    tx.Commit() // 提交

    // 查询
    rows, err := db.Query("select id, name, price from t_products")
    if err != nil {
        log.Fatal(err)
        return
    }
    defer rows.Close()
    for rows.Next() {
        var id int
        var name string
        var price float64
        err = rows.Scan(&id, &name, &price)
        if err != nil {
            log.Fatal(err)
            return
        }
        priceStr := fmt.Sprintf("%.2f", price)
        fmt.Println(id, name, priceStr)
    }

    stmt, _ = db.Prepare("select name,price from t_products where id=?")
    defer stmt.Close()
    var name string
    var price float64
    stmt.QueryRow("6").Scan(&name, &price)
    priceStr := fmt.Sprintf("%.2f", price)
    fmt.Println("--------------")
    fmt.Println(name, priceStr)

    // 删除
    stmt, _ = db.Prepare("delete from t_products where id=?")
    stmt.Exec(9)
}

操作步骤:

  • 1、在 Mac 中打开活动监视器(在 Finder 的「应用程序」中搜索「活动监视器」可以找到)。
  • 2、在「活动监视器」窗口右上角的搜索框里输入「audio」,此时可以搜索到「coreaudiod」进程。
  • 3、选中「coreaudiod」进程,点击「活动监视器」窗口左上角的「退出进程」按钮,在弹出的对话框中点击「退出」。
  • 4、「coreaudiod」进程退出后会自动重启,这时声音就恢复了。

package main

import (
    "fmt"
    "log"
    "net/http"
    "time"
)

func writeCookie(w http.ResponseWriter, r *http.Request) {
    expiration := time.Now()
    expiration = expiration.AddDate(0, 0, 3)

    cookie := http.Cookie{Name: "username", Value: "wufeng", Expires: expiration}
    http.SetCookie(w, &cookie)
    fmt.Fprintf(w, "write cookie success")
}

func readCookie(w http.ResponseWriter, r *http.Request) {
    fmt.Fprint(w, "<html>")
    cookie, _ := r.Cookie("username")
    fmt.Fprint(w, cookie.Value)
    fmt.Fprint(w, "<br />")
    fmt.Fprint(w, "</html>")
}
func main() {
    http.HandleFunc("/writeCookie", writeCookie)
    http.HandleFunc("/readCookie", readCookie)
    fmt.Println("服务器已启动")
    err := http.ListenAndServe(":9090", nil)

    if err != nil {
        log.Fatal(err)
    }
}