-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
38 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,39 @@ | ||
# zonce | ||
自定义once包,解决初始化错误的情况 | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"log" | ||
) | ||
import "github.com/Xuzan9396/zonce" | ||
|
||
func main() { | ||
var onces zonce.Once | ||
// 第一次执行中出现了失败,模拟初始化失败了 | ||
err := onces.Do(func() error { | ||
log.Println("执行一次1") | ||
return errors.New("模拟实例化中间出现错误") | ||
}) | ||
log.Println(err) | ||
|
||
// 第二次成功了 | ||
err = onces.Do(func() error { | ||
log.Println("执行一次2") | ||
return nil | ||
}) | ||
log.Println(err) | ||
|
||
|
||
// 第三次不会重新实例化了 | ||
err = onces.Do(func() error { | ||
log.Println("执行一次3") | ||
return nil | ||
}) | ||
log.Println(err) | ||
|
||
} | ||
|
||
``` |