{"id":18938312,"url":"https://github.com/faiface/mainthread","last_synced_at":"2025-11-06T08:03:26.718Z","repository":{"id":16256308,"uuid":"79488236","full_name":"faiface/mainthread","owner":"faiface","description":"Run stuff on the main thread in Go","archived":false,"fork":false,"pushed_at":"2023-10-07T23:24:09.000Z","size":7,"stargazers_count":78,"open_issues_count":3,"forks_count":18,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-12-10T07:10:58.165Z","etag":null,"topics":["go","golang","library","mainthread"],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/faiface.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}},"created_at":"2017-01-19T19:40:34.000Z","updated_at":"2024-11-20T19:01:44.000Z","dependencies_parsed_at":"2022-07-22T00:47:00.851Z","dependency_job_id":null,"html_url":"https://github.com/faiface/mainthread","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/faiface%2Fmainthread","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/faiface%2Fmainthread/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/faiface%2Fmainthread/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/faiface%2Fmainthread/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/faiface","download_url":"https://codeload.github.com/faiface/mainthread/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":230431102,"owners_count":18224655,"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":["go","golang","library","mainthread"],"created_at":"2024-11-08T12:14:03.813Z","updated_at":"2025-11-06T08:03:26.674Z","avatar_url":"https://github.com/faiface.png","language":"Go","readme":"# mainthread [![GoDoc](https://godoc.org/github.com/faiface/mainthread?status.svg)](http://godoc.org/github.com/faiface/mainthread) [![Report card](https://goreportcard.com/badge/github.com/faiface/mainthread)](https://goreportcard.com/report/github.com/faiface/mainthread)\n\nPackage mainthread allows you to run code on the main operating system thread.\n\n`go get github.com/faiface/mainthread`\n\nOperating systems often require, that code which deals with windows and graphics has to run on the\nmain thread. This is however somehow challenging in Go due to Go's concurrent nature.\n\nThis package makes it easily possible.\n\nAll you need to do is put your main code into a separate function and call `mainthread.Run` from\nyour real main, like this:\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\n\t\"github.com/faiface/mainthread\"\n)\n\nfunc run() {\n\t// now we can run stuff on the main thread like this\n\tmainthread.Call(func() {\n\t\tfmt.Println(\"printing from the main thread\")\n\t})\n\tfmt.Println(\"printing from another thread\")\n}\n\nfunc main() {\n\tmainthread.Run(run) // enables mainthread package and runs run in a separate goroutine\n}\n```\n\n## More functions\n\nIf you don't wish to wait until a function finishes running on the main thread, use\n`mainthread.CallNonBlock`:\n\n```go\nmainthread.CallNonBlock(func() {\n\tfmt.Println(\"i'm in the main thread\")\n})\nfmt.Println(\"but imma be likely printed first, cuz i don't wait\")\n```\n\nIf you want to get some value returned from the main thread, you can use `mainthread.CallErr` or\n`mainthread.CallVal`:\n\n```go\nerr := mainthread.CallErr(func() error {\n\treturn nil // i don't do nothing wrong\n})\nval := mainthread.CallVal(func() interface{} {\n\treturn 42 // the meaning of life, universe and everything\n})\n```\n\nIf `mainthread.CallErr` or `mainthread.CallVal` aren't sufficient for you, you can just assign\nvariables from within the main thread:\n\n```go\nvar x, y int\nmainthread.Call(func() {\n\tx, y = 1, 2\n})\n```\n\nHowever, be careful with `mainthread.CallNonBlock` when dealing with local variables.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffaiface%2Fmainthread","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffaiface%2Fmainthread","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffaiface%2Fmainthread/lists"}