浏览文件
identity.go
28 行 · 566 B历史下载| 1 | package version |
| 2 | |
| 3 | import ( |
| 4 | "crypto/sha256" |
| 5 | "fmt" |
| 6 | "io" |
| 7 | "os" |
| 8 | "sync" |
| 9 | ) |
| 10 | |
| 11 | // ExecutableIdentity is fixed when first read. A running service must not adopt |
| 12 | // the identity of a replacement subsequently installed at the same path. |
| 13 | var ExecutableIdentity = sync.OnceValues(func() (string, error) { |
| 14 | path, err := os.Executable() |
| 15 | if err != nil { |
| 16 | return "", err |
| 17 | } |
| 18 | f, err := os.Open(path) |
| 19 | if err != nil { |
| 20 | return "", err |
| 21 | } |
| 22 | defer f.Close() |
| 23 | h := sha256.New() |
| 24 | if _, err = io.Copy(h, f); err != nil { |
| 25 | return "", err |
| 26 | } |
| 27 | return fmt.Sprintf("%x", h.Sum(nil)), nil |
| 28 | }) |

