-
Notifications
You must be signed in to change notification settings - Fork 316
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add example Logging and Embed Directive
- Loading branch information
Showing
30 changed files
with
297 additions
and
147 deletions.
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
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
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,41 @@ | ||
// `//go:embed` 是一个 [编译器指令](https://pkg.go.dev/cmd/compile#hdr-Compiler_Directives), | ||
// 它允许程序在构建时将任意文件和文件夹包含在 Go 二进制文件中。在这里阅读有关嵌入指令的更多信息:[这里](https://pkg.go.dev/embed)。 | ||
package main | ||
|
||
// 导入 `embed` 包;如果您不使用该包中的任何导出标识符,可以使用 `_ "embed"` 进行空白导入。 | ||
import ( | ||
"embed" | ||
) | ||
|
||
// `embed` 指令接受相对于包含 Go 源文件的目录的路径。 | ||
// 该指令将文件的内容嵌入到紧随其后的 `string` 变量中。 | ||
// | ||
//go:embed folder/single_file.txt | ||
var fileString string | ||
|
||
// 将文件的内容嵌入到一个 `[]byte` 中。 | ||
// | ||
//go:embed folder/single_file.txt | ||
var fileByte []byte | ||
|
||
// 我们还可以使用通配符嵌入多个文件甚至文件夹。 | ||
// 这将使用 [embed.FS 类型](https://pkg.go.dev/embed#FS)的变量, | ||
// 该类型实现了一个简单的虚拟文件系统。 | ||
// | ||
//go:embed folder/single_file.txt | ||
//go:embed folder/*.hash | ||
var folder embed.FS | ||
|
||
func main() { | ||
|
||
// 打印出 `single_file.txt` 的内容。 | ||
print(fileString) | ||
print(string(fileByte)) | ||
|
||
// 从嵌入的文件夹中检索一些文件。 | ||
content1, _ := folder.ReadFile("folder/file1.hash") | ||
print(string(content1)) | ||
|
||
content2, _ := folder.ReadFile("folder/file2.hash") | ||
print(string(content2)) | ||
} |
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,2 @@ | ||
69526bd78ac861c85bb12b96e9f1273e8aecc5a6 | ||
6m2ll-D52BB |
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,13 @@ | ||
# 使用这些命令来运行示例。 | ||
#(注意:由于 Go Playground 的限制,这个示例只能在您的本地机器上运行。) | ||
$ mkdir -p folder | ||
$ echo "hello go" > folder/single_file.txt | ||
$ echo "123" > folder/file1.hash | ||
$ echo "456" > folder/file2.hash | ||
|
||
$ go run embed-directive.go | ||
hello go | ||
hello go | ||
123 | ||
456 | ||
|
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 @@ | ||
123 |
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 @@ | ||
456 |
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 @@ | ||
hello go |
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,60 @@ | ||
// Go标准库提供了直观的工具用于从Go程序输出日志 | ||
// 使用 [log](https://pkg.go.dev/log) 包进行自由格式输出 | ||
// 使用 [log/slog](https://pkg.go.dev/log/slog) 包进行结构化输出。 | ||
|
||
package main | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"log" | ||
"os" | ||
|
||
"log/slog" | ||
) | ||
|
||
func main() { | ||
|
||
// 只需调用 `log` 包中的 `Println` 等函数即可使用 _标准_ logger。 | ||
// 它已经预先配置为将日志输出到 `os.Stderr`。 | ||
// 像 `Fatal*` 或 `Panic*` 这样的附加方法将在记录日志后退出程序。 | ||
log.Println("standard logger") | ||
|
||
// 日志记录器可以使用 _flags_ 进行配置,以设置它们的输出格式。 | ||
// 默认情况下,标准记录器已设置了 `log.Ldate` 和 `log.Ltime` 标志, | ||
// 并将它们收集在 `log.LstdFlags` 中。 | ||
// 我们可以更改其标志以发出微秒精度的时间,例如: | ||
log.SetFlags(log.LstdFlags | log.Lmicroseconds) | ||
log.Println("with micro") | ||
|
||
// 它还支持发出调用 log` 函数的文件名和行号。 | ||
log.SetFlags(log.LstdFlags | log.Lshortfile) | ||
log.Println("with file/line") | ||
|
||
// 可能会有用创建一个自定义记录器并在各处传递它。 | ||
// 创建新记录器时,我们可以设置一个 _前缀_ 来区分其输出和其他日志记录器。 | ||
mylog := log.New(os.Stdout, "my:", log.LstdFlags) | ||
mylog.Println("from mylog") | ||
|
||
// 我们可以使用 `SetPrefix` 方法在现有的记录器(包括标准记录器)上设置前缀。 | ||
mylog.SetPrefix("ohmy:") | ||
mylog.Println("from mylog") | ||
|
||
// 日志记录器可以具有自定义的输出目标;任何 `io.Writer` 都可以使用。 | ||
var buf bytes.Buffer | ||
buflog := log.New(&buf, "buf:", log.LstdFlags) | ||
|
||
// 这个调用将日志输出写入到 `buf` 中. | ||
buflog.Println("hello") | ||
|
||
// 这将实际上显示在标准输出上。 | ||
fmt.Print("from buflog:", buf.String()) | ||
|
||
// `slog 包提供了 _结构化_ 的日志输出。例如,以 JSON 格式记录日志非常直接。 | ||
jsonHandler := slog.NewJSONHandler(os.Stderr, nil) | ||
myslog := slog.New(jsonHandler) | ||
myslog.Info("hi there") | ||
|
||
// 除了 `msg` 之外,`slog` 输出还可以包含任意数量的键值对。 | ||
myslog.Info("hello again", "key", "val", "age", 25) | ||
} |
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,2 @@ | ||
38a7ef451859bb4c163df938b3a9d0e5ac293bef | ||
Qd0uCqBlYUn |
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,17 @@ | ||
# 示例输出; | ||
# 发出的日期和时间将取决于示例运行的时间。 | ||
$ go run logging.go | ||
2023/08/22 10:45:16 standard logger | ||
2023/08/22 10:45:16.904141 with micro | ||
2023/08/22 10:45:16 logging.go:40: with file/line | ||
my:2023/08/22 10:45:16 from mylog | ||
ohmy:2023/08/22 10:45:16 from mylog | ||
from buflog:buf:2023/08/22 10:45:16 hello | ||
|
||
# 这些被换行以便在网站上更清晰地呈现; | ||
# 实际上它们是在单行上发出的。 | ||
{"time":"2023-08-22T10:45:16.904166391-07:00", | ||
"level":"INFO","msg":"hi there"} | ||
{"time":"2023-08-22T10:45:16.904178985-07:00", | ||
"level":"INFO","msg":"hello again", | ||
"key":"val","age":25} |
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
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
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
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,15 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>Go by Example: Not Found</title> | ||
<link rel=stylesheet href="site.css"> | ||
</head> | ||
<body> | ||
<div id="intro"> | ||
<h2><a href="./">Go by Example</a></h2> | ||
<p>Sorry, we couldn't find that! Check out the <a href="./">home page</a>?</p> | ||
{{ template "footer" }} | ||
</div> | ||
</body> | ||
</html> |
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
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,5 +1,5 @@ | ||
{{define "footer"}} | ||
<p class="footer"> | ||
<a href="https://twitter.com/mmcgrana">@mmcgrana</a> 和<a href="https://eli.thegreenplace.net">Eli Bendersky</a>编写 | <a href="https://github.com/gobyexample-cn">gobyexample-cn</a> 翻译 | <a href="https://github.com/gobyexample-cn/gobyexample/issues">反馈</a> | <a href="https://github.com/gobyexample-cn/gobyexample">源码</a> | <a href="https://github.com/mmcgrana/gobyexample#license">license</a> </p> | ||
by <a href="https://markmcgranaghan.com">Mark McGranaghan</a> and <a href="https://eli.thegreenplace.net">Eli Bendersky</a> | <a href="https://github.com/mmcgrana/gobyexample">source</a> | <a href="https://github.com/mmcgrana/gobyexample#license">license</a> | ||
</p> | ||
{{end}} |
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
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
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,4 +1,4 @@ | ||
#!/bin/bash | ||
#!/usr/bin/env bash | ||
|
||
set -e | ||
|
||
|
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,4 +1,4 @@ | ||
#!/bin/bash | ||
#!/usr/bin/env bash | ||
|
||
TRAPPING=0 | ||
trap "{ echo finishing; TRAPPING=1; }" SIGINT | ||
|
Oops, something went wrong.