Skip to content

Commit

Permalink
go一些技巧
Browse files Browse the repository at this point in the history
Signed-off-by: kaisawind <wind.kaisa@gmail.com>
  • Loading branch information
kaisawind committed Jun 15, 2020
1 parent 09b570b commit f781117
Showing 1 changed file with 179 additions and 0 deletions.
179 changes: 179 additions & 0 deletions source/_posts/2020-06-15-go.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
---
layout: post
title: "go一些技巧"
date: 2020-06-15 09:21:12 +0800
categories: [golang]
excerpt_separator: <!--more-->
---
go一些技巧
<!--more-->

1. map第二个返回值
go语言的map有第二个返回值,能够判断在map中key是否存在
```go
value, ok := kv[key]
if !ok {
// do something
}
```

2. `_`无视其中一个值
使用`_`可以无视多个值中的其中几个或全部
```go
_, ok := kv[key]
if !ok {
// do something
}
```
无视全部值
```go
_, _ := kv[key]
```

3. 在切片后追加元素
```go
var arr []int
arr = append(arr, 1)
```


4. 在切片前追加元素
```go
var arr []int
arr = append([]int{1}, arr...)
```

5. 在切片后追加多个元素
```go
var arr []int
arr = append(arr, []int{1, 2, 3, 4, 5}...)
```

6. 切片越界

不会越界,是空切片
```go
fmt.Println(arr[len(arr):])
```

会越界
```go
fmt.Println(arr[len(arr)+1:])
```

会越界
```go
fmt.Println(arr[len(arr)])
```

7. 弃用标记
```go
// Execer is an optional interface that may be implemented by a Conn.
//
// If a Conn does not implement Execer, the sql package's DB.Exec will
// first prepare a query, execute the statement, and then close the
// statement.
//
// Exec may return ErrSkip.
//
// Deprecated: Drivers should implement ExecerContext instead (or additionally).
type Execer interface {
Exec(query string, args []Value) (Result, error)
}
```

8. BUG标记
```go
// BUG(who): 我是bug说明
```

9. 包注释
单独doc.go文件
```go
package aaa

// Execer is an optional interface that may be implemented by a Conn.
//
// If a Conn does not implement Execer, the sql package's DB.Exec will
// first prepare a query, execute the statement, and then close the
// statement.
//
// Exec may return ErrSkip.
```

10. Example
文件名`example_xxx_test.go`
包名`xxx_test`
函数名`func Example[FuncName][_tag]()`
函数尾 `// Output:`
```go
// 文件必须放在 banana包目录下, 名字必须为example_xxx_test.go

// Package banana_test 为banana包的示例
package banana_test

// 此注释将会被展示在页面上
// 此函数将被展示在OverView区域
func Example() {
fmt.Println("Hello OverView")

// Output:
// Hello OverView
}
```

11. UnitTest

文件名`xxx_test.go`
包名`xxx_test`
函数名`func Test[FuncName][_tag]()`

```go
package server_test

import (
"testing"
"time"
)
func TestServerTimeLayout1(t *testing.T) {
some := time.Now().Format(types.TimeLayout1)
t.Log(some)
}
```

12. BenchmarkTest
文件名`xxx_test.go`
包名`xxx_test`
函数名`func Benchmark[FuncName][_tag]()`

```go
package types_test

func BenchmarkDataMarshal(b *testing.B) {
for i := 0; i < b.N; i++ {
data := Data{
MsgType: DeviceReq,
Data: []*DeviceS{
{
DeviceID: "abc-123_&%S",
ServiceID: "discrete",
EventTime: "123456",
Data: map[string]interface{}{
"LD_14.XY": "0",
"LD_15.XY": 0,
"LD_16.XY": int64(64),
"LD_17.XY": true,
"LD_18.XY": 123.456,
},
},
},
}
_, err := json.Marshal(data)
if err != nil {
b.FailNow()
}
// fmt.Println("json", string(bytes))
}

}
```

0 comments on commit f781117

Please sign in to comment.