search

command
v0.0.0-...-c576ecb Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 15, 2017 License: MIT Imports: 4 Imported by: 0

README

笔记

空结构

type defaultMatcher struct {}

空结构类型在创建实例的时,不会分配任何内存。这种结构很适合创建没有任何状态的类型。

闭包

有了闭包,函数可以直接访问到那些没有作为参数传入的变量。

nil 和 empty slice

有的时候我们需要创建一个 nil slice,创建一个 nil slice 的方法是声明它但不初始化它:

var slice []int

创建一个 nil slice 是创建 slice 最基本的方法,很多标准库和内建函数都可以使用它。当我们想要表示一个并不存在的 slice 时它变得非常有用,比如一个返回 slice 的函数中发生异常的时候。 创建 empty slice 的方法就是声明并初始化一下:

// 使用 make 创建
silce := make([]int, 0)

// 使用 slice 字面值创建
slice := []int{}

empty slice 包含0个元素并且底层数组没有分配存储空间。当我们想要表示一个空集合时它很有用处,比如一个数据库查询返回0个结果。 不管我们用 nil slice 还是 empty slice,内建函数 append,len和cap的工作方式完全相同。

其他知识点

main包的问题

在main包中新建两个文件,一个定义一个struct, 一个定义main方法,在main中引用这个struct, go run main.go 的时候会报没有这个结构体。

解决方案:go run main.go T.go 将struct文件名字加到go run 的后面,就可以运行了, go run 的时候只会加载已经添加了引用的文件。

go build

go build 可以直接指定包, 如:

go build igthub.com/goinaction/code/chapter3/wordcount

也可以使用通配符,3个点表示匹配所有字符串

go build igthub.com/goinaction/code/chapter3/...

问题

mysql row 问题

rows.Err() : 在for中先取值,然后需要在下面判断这个rows.Err() 是不是因为网络原因没读完,如果没读完需要记录日志等信息。

package main
import (
    "database/sql"
    "fmt"
    _ "github.com/go-sql-driver/mysql"
)
func main() {
    // Open database connection
    db, err := sql.Open("mysql", "user:password@/dbname")
    if err != nil {
        panic(err.Error())  // Just for example purpose. You should use proper error handling instead of panic
    }
    defer db.Close()
    // Execute the query
    rows, err := db.Query("SELECT * FROM table")
    if err != nil {
        panic(err.Error()) // proper error handling instead of panic in your app
    }
    // Get column names
    columns, err := rows.Columns()
    if err != nil {
        panic(err.Error()) // proper error handling instead of panic in your app
    }
    // Make a slice for the values
    values := make([]sql.RawBytes, len(columns))
    // rows.Scan wants '[]interface{}' as an argument, so we must copy the
    // references into such a slice
    // See http://code.google.com/p/go-wiki/wiki/InterfaceSlice for details
    scanArgs := make([]interface{}, len(values))
    for i := range values {
        scanArgs[i] = &values[i]
    }
    // Fetch rows
    for rows.Next() {
        // get RawBytes from data
        err = rows.Scan(scanArgs...)
        if err != nil {
            panic(err.Error()) // proper error handling instead of panic in your app
        }
        // Now do something with the data.
        // Here we just print each column as a string.
        var value string
        for i, col := range values {
            // Here we can check if the value is nil (NULL value)
            if col == nil {
                value = "NULL"
            } else {
                value = string(col)
            }
            fmt.Println(columns[i], ": ", value)
        }
        fmt.Println("-----------------------------------")
    }
    if err = rows.Err(); err != nil {
        panic(err.Error()) // proper error handling instead of panic in your app
    }
}

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL