{"id":17671243,"url":"https://github.com/xgfone/go-atexit","last_synced_at":"2025-10-20T12:30:28.434Z","repository":{"id":51647827,"uuid":"371850112","full_name":"xgfone/go-atexit","owner":"xgfone","description":"Manage the exit functions of the program.","archived":false,"fork":false,"pushed_at":"2024-05-13T06:36:27.000Z","size":61,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-12-11T21:42:01.570Z","etag":null,"topics":["atexit","exit","go","golang","init"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/xgfone.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-05-29T00:50:54.000Z","updated_at":"2024-05-13T06:33:48.000Z","dependencies_parsed_at":"2024-05-13T07:45:43.645Z","dependency_job_id":null,"html_url":"https://github.com/xgfone/go-atexit","commit_stats":null,"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xgfone%2Fgo-atexit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xgfone%2Fgo-atexit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xgfone%2Fgo-atexit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xgfone%2Fgo-atexit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/xgfone","download_url":"https://codeload.github.com/xgfone/go-atexit/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":237331706,"owners_count":19292611,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["atexit","exit","go","golang","init"],"created_at":"2024-10-24T03:13:38.116Z","updated_at":"2025-10-20T12:30:23.112Z","avatar_url":"https://github.com/xgfone.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Go AtExit [![Build Status](https://github.com/xgfone/go-atexit/actions/workflows/go.yml/badge.svg)](https://github.com/xgfone/go-atexit/actions/workflows/go.yml) [![GoDoc](https://pkg.go.dev/badge/github.com/xgfone/go-atexit)](https://pkg.go.dev/github.com/xgfone/go-atexit) [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg?style=flat-square)](https://raw.githubusercontent.com/xgfone/go-atexit/master/LICENSE)\n\nThe package `atexit` is used to manage the exit functions of the program. Support `Go1.8+`.\n\n## Install\n```shell\n$ go get -u github.com/xgfone/go-atexit\n```\n\n## Example\n```go\npackage main\n\nimport (\n\t\"flag\"\n\t\"log\"\n\t\"os\"\n\n\t\"github.com/xgfone/go-atexit\"\n)\n\nvar logfile = flag.String(\"logfile\", \"\", \"the log file path\")\n\nfunc init() {\n\t// Register the exit functions\n\tatexit.OnExitWithPriority(1, func() { log.Println(\"the program exits\") })\n\tatexit.OnExit(func() { log.Println(\"do something to clean\") })\n\n\t// Register the init functions.\n\tatexit.OnInit(flag.Parse)\n\tatexit.OnInit(func() {\n\t\tif *logfile != \"\" {\n\t\t\tfile, err := os.OpenFile(*logfile, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)\n\t\t\tif err != nil {\n\t\t\t\tlog.Println(err)\n\t\t\t\tatexit.Exit(1)\n\t\t\t} else {\n\t\t\t\tlog.SetOutput(file)\n\t\t\t}\n\n\t\t\t// Close the file before the program exits.\n\t\t\tatexit.OnExitWithPriority(0, func() {\n\t\t\t\tlog.Println(\"close the log file\")\n\t\t\t\tfile.Close()\n\t\t\t})\n\t\t}\n\t})\n}\n\nfunc main() {\n\tatexit.Init()\n\n\tlog.Println(\"do jobs ...\")\n\n\tatexit.Exit(0)\n\n\t// $ go run main.go\n\t// 2021/05/29 08:29:14 do jobs ...\n\t// 2021/05/29 08:29:14 do something to clean\n\t// 2021/05/29 08:29:14 the program exits\n\t//\n\t// $ go run main.go -logfile test.log\n\t// $ cat test.log\n\t// 2021/05/29 08:29:19 do jobs ...\n\t// 2021/05/29 08:29:19 do something to clean\n\t// 2021/05/29 08:29:19 the program exits\n\t// 2021/05/29 08:29:19 close the log file\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxgfone%2Fgo-atexit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxgfone%2Fgo-atexit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxgfone%2Fgo-atexit/lists"}