const ( // DebugLevel logs are typically voluminous, and are usually disabled in production. DebugLevel Level = iota - 1 // InfoLevel is the default logging priority. InfoLevel // WarnLevel logs are more important than Info, but don't need individual human review. WarnLevel // ErrorLevel logs are high-priority. If an application is running smoothly, it shouldn't generate any error-level logs. ErrorLevel // DPanicLevel logs are particularly important errors. In development the logger panics after writing the message. DPanicLevel // PanicLevel logs a message, then panics. PanicLevel // FatalLevel logs a message, then calls os.Exit(1). FatalLevel
funcmain() { // For some users, the presets offered by the NewProduction, NewDevelopment, // and NewExample constructors won't be appropriate. For most of those // users, the bundled Config struct offers the right balance of flexibility // and convenience. (For more complex needs, see the AdvancedConfiguration // example.) // // See the documentation for Config and zapcore.EncoderConfig for all the // available options. rawJSON := []byte(`{ "level": "debug", "encoding": "json", "outputPaths": ["stdout", "/tmp/logs"], "errorOutputPaths": ["stderr"], "initialFields": {"foo": "bar"}, "encoderConfig": { "messageKey": "message", "levelKey": "level", "levelEncoder": "lowercase" } }`)
var cfg zap.Config if err := json.Unmarshal(rawJSON, &cfg); err != nil { panic(err) } logger, err := cfg.Build() if err != nil { panic(err) } defer logger.Sync()
funcmain() { // The bundled Config struct only supports the most common configuration // options. More complex needs, like splitting logs between multiple files // or writing to non-file outputs, require use of the zapcore package. // // In this example, imagine we're both sending our logs to Kafka and writing // them to the console. We'd like to encode the console output and the Kafka // topics differently, and we'd also like special treatment for // high-priority logs.
// Assume that we have clients for two Kafka topics. The clients implement // zapcore.WriteSyncer and are safe for concurrent use. (If they only // implement io.Writer, we can use zapcore.AddSync to add a no-op Sync // method. If they're not safe for concurrent use, we can add a protecting // mutex with zapcore.Lock.) topicDebugging := zapcore.AddSync(ioutil.Discard) topicErrors := zapcore.AddSync(ioutil.Discard)
// High-priority output should also go to standard error, and low-priority // output should also go to standard out. consoleDebugging := zapcore.Lock(os.Stdout) consoleErrors := zapcore.Lock(os.Stderr)
// Optimize the Kafka output for machine consumption and the console output // for human operators. kafkaEncoder := zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()) consoleEncoder := zapcore.NewConsoleEncoder(zap.NewDevelopmentEncoderConfig())
// Join the outputs, encoders, and level-handling functions into // zapcore.Cores, then tee the four cores together. core := zapcore.NewTee( zapcore.NewCore(kafkaEncoder, topicErrors, highPriority), zapcore.NewCore(consoleEncoder, consoleErrors, highPriority), zapcore.NewCore(kafkaEncoder, topicDebugging, lowPriority), zapcore.NewCore(consoleEncoder, consoleDebugging, lowPriority), )
// From a zapcore.Core, it's easy to construct a Logger. logger := zap.New(core) defer logger.Sync() logger.Info("constructed a logger") }
d := time.Now().Add(50 * time.Millisecond) ctx, cancel := context.WithDeadline(context.Background(), d)
// Even though ctx will be expired, it is good practice to call its // cancelation function in any case. Failure to do so may keep the // context and its parent alive longer than necessary. defer cancel()
select { case <-time.After(1 * time.Second): fmt.Println("overslept") case <-ctx.Done(): fmt.Println(ctx.Err()) }
// An emptyCtx is never canceled, has no values, and has no deadline. It is not // struct{}, since vars of this type must have distinct addresses. type emptyCtx int
func(*emptyCtx) Deadline() (deadline time.Time, ok bool) { return }
m.Unlock()对m解锁,如果m未加锁会导致运行时错误:panic: sync: unlock of unlocked mutex
type RWMutex
读锁:又叫共享锁,可以并发读
1 2 3 4 5 6 7
type RWMutex struct { w Mutex // held if there are pending writers writerSem uint32// 写锁需要等待读锁释放的信号量 readerSem uint32// 读锁需要等待写锁释放的信号量 readerCount int32// 读锁后面挂起了多少个写锁申请 readerWait int32// 已释放了多少个读锁 }
funcmain() { // A *DB is a pool of connections. Call Conn to reserve a connection for // exclusive use. conn, err := db.Conn(ctx) if err != nil { log.Fatal(err) } defer conn.Close() // Return the connection to the pool. id := 41 result, err := conn.ExecContext(ctx, `UPDATE balances SET balance = balance + 10 WHERE user_id = ?;`, id) if err != nil { log.Fatal(err) } rows, err := result.RowsAffected() if err != nil { log.Fatal(err) } if rows != 1 { log.Fatalf("expected single row affected, got %d rows affected", rows) } }
type DBStats struct { MaxOpenConnections int// Maximum number of open connections to the database.
// Pool Status OpenConnections int// The number of established connections both in use and idle. InUse int// The number of connections currently in use. Idle int// The number of idle connections.
// Counters WaitCount int64// The total number of connections waited for. WaitDuration time.Duration // The total time blocked waiting for a new connection. MaxIdleClosed int64// The total number of connections closed due to SetMaxIdleConns. MaxIdleTimeClosed int64// The total number of connections closed due to SetConnMaxIdleTime. MaxLifetimeClosed int64// The total number of connections closed due to SetConnMaxLifetime. }
// Dest is a pointer to the value that will be set to the result of the // stored procedure's OUTPUT parameter. Dest interface{}
// In is whether the parameter is an INOUT parameter. If so, the input value to the stored // procedure is the dereferenced value of Dest's pointer, which is then replaced with // the output value. In bool // contains filtered or unexported fields }
type Result interface { // LastInsertId returns the integer generated by the database // in response to a command. Typically this will be from an // "auto increment" column when inserting a new row. Not all // databases support this feature, and the syntax of such // statements varies. LastInsertId() (int64, error)
// RowsAffected returns the number of rows affected by an // update, insert, or delete. Not every database or database // driver may support this. RowsAffected() (int64, error) }
Result 汇总了已执行的SQL语句。
type Row
1 2 3 4 5
type Row struct { // One of these two will be non-nil: err error// deferred error for easy chaining rows *Rows }
type TxOptions struct { // Isolation is the transaction isolation level. // If zero, the driver or database's default level is used. Isolation IsolationLevel ReadOnly bool }
More detailed explanatory text, if necessary. Wrap it to about 72 characters or so. Further paragraphs come after blank lines. - Bullet points are okay, too - Use a hanging indent
BREAKING CHANGE: isolate scope bindings definition has changed. To migrate the code follow the example below: Before: scope: { myAttr: ‘attribute’, } After: scope: { myAttr: ‘@’, } The removed inject wasn’t generaly useful for directives so there should be no code using it.
寄存器有两种概念,逻辑上的和物理上的,分别是架构相关寄存器(architectural register)和物理寄存器(physical register)。前者是指令集(ISA)提供给编译器可见的,相当于 API 接口规范,一共 16 个通用寄存器;后者是硬件上实际设计的,软件领域不直接接触。最新的 CPU 可能有上百个实际的物理寄存器。当然了,对软件开发人员来说,我们只需要关注逻辑上的通用寄存器。
这 16 个逻辑上的通用寄存器如下所示:
寄存器 16bit / 32bit / 64bit
主要用途
编号
ax / eax / rax
累加器
0
cx / ecx / rcx
计数
1
dx / edx / rdx
I/O 指针
2
bx / ebx / rbx
DS 段的数据指针
3
sp / esp / rsp
堆栈指针寄存器在堆栈操作中使用,PUSH 和 POP 指令是从 SP 寄存器得到现行堆栈段的段内偏移量,所以称 SP 寄存器为堆栈指针:stack pointer,SP 始终指向栈顶。
4
bp / ebp / rbp
BP 与 SS 连用,为访问现行堆栈段提供方便。通常 BP 寄存器在间接寻址中使用,操作数在堆栈段中,由 SS 段寄存器和 BP 组合形成操作数的地址,即 BP 中存放现行堆栈段中一个数据区的“基址”的偏移量,所以称 BP 为基址指针:base pointer
SUB AL, imm8 SUB AX, imm16 SUB EAX, imm32 SUB r/m8, imm8 SUB r/m16,imm16 SUB r/m32,imm32 SUB r/m16, imm8 SUB r/m32, imm8 SUB r/m8, r8 SUB r/m16, r16 SUB r/m32, r32 SUB r8, r/m8 SUB r16, r/m16 SUB r32, r/m32
作用:目标操作数减去源操作数,将结果存回目标操作数
示例:
1 2 3
mov eax,3 mov ebx,1 sub eax,ebx ; eax = 2
AND 指令
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
AND DST,SRC
AND AL, imm8 AND AX, imm16 AND EAX, imm32 AND r/m8, imm8 AND r/m16,imm16 AND r/m32,imm32 AND r/m16, imm8 AND r/m32, imm8 AND r/m8, r8 AND r/m16, r16 AND r/m32, r32 AND r8, r/m8 AND r16, r/m16 AND r32, r/m32
OR AL, imm8 OR AX, imm16 OR EAX, imm32 OR r/m8, imm8 OR r/m16,imm16 OR r/m32,imm32 OR r/m16, imm8 OR r/m32, imm8 OR r/m8, r8 OR r/m16, r16 OR r/m32, r32 OR r8, r/m8 OR r16, r/m16 OR r32, r/m32
作用:DST 和 SRC 按位逻辑或操作,将结果存回 DST
示例:
1 2 3
mov eax,100 mov ebx,1 or eax,ebx ; eax = 0x00000101