{"id":20766193,"url":"https://github.com/navono/golangsimpletutorial","last_synced_at":"2025-03-11T18:48:32.953Z","repository":{"id":74496712,"uuid":"117427596","full_name":"navono/golangSimpleTutorial","owner":"navono","description":"A sample source demonstrate Golang","archived":false,"fork":false,"pushed_at":"2018-01-28T11:46:26.000Z","size":20,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-26T03:36:25.350Z","etag":null,"topics":["golang"],"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/navono.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":"2018-01-14T12:21:22.000Z","updated_at":"2018-01-14T12:29:36.000Z","dependencies_parsed_at":null,"dependency_job_id":"3f43d4af-42aa-424d-b5bb-c2892c0a991f","html_url":"https://github.com/navono/golangSimpleTutorial","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/navono%2FgolangSimpleTutorial","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/navono%2FgolangSimpleTutorial/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/navono%2FgolangSimpleTutorial/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/navono%2FgolangSimpleTutorial/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/navono","download_url":"https://codeload.github.com/navono/golangSimpleTutorial/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243093958,"owners_count":20235467,"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":["golang"],"created_at":"2024-11-17T11:21:55.842Z","updated_at":"2025-03-11T18:48:32.926Z","avatar_url":"https://github.com/navono.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# GolangSimpleTutorial\n![](https://img.shields.io/badge/language-Golang-orange.svg)\n[![](https://img.shields.io/badge/license-MIT-000000.svg)](https://github.com/navono/golangSimpleTutorial.git/blob/master/LICENSE)\n\n# 环境配置\n`VS Code`下安装`Go`插件后，提示需要安装一些工具，但是在墙内会经常导致失败。可以[在此](https://pan.baidu.com/s/1mjLPKfe)下载已经编译好的`Windows`版本。将这些二进制文件放置到`GOPATH\\bin`目录下即可。\n\n# 运行\n\u003e go run main.go\n\n\n# 解释说明\n## goroutine\n`goroutine`是什么？是`OS线程`？它是如何工作的？我们可以随意创建任意数量吗？\n\u003e `goroutine`是`Go`特有的，它们不是`OS线程`，或者说连线程都不是。\n  \u003cbr\u003e它们由`Go`的`runtime`统一管理，一种高层级的抽象的`coroutine`。`coroutine`是并发的`subroutine`（functions, closures or methods in Go），同时是不可抢占的（也就是不能被中断），相反，`coroutine`是可重入（reentry）和挂起的（suspension）。\n  \u003cbr\u003e`goroutine`不支持可重入和挂起，这些统一由`Go`的`runtime`管理。`runtime`观察`goroutine`的行为，在`goroutine`阻塞时，对其挂起，非阻塞时，恢复其运行。\n  \u003cbr\u003e由此看出，`goroutine`是一种特殊的`coroutine`。\n\n`goroutine`是隐式并发的。但是这并不是说并发是`coroutine`的一个属性。还是需要有个宿主（Host）来同时管理多个`coroutine`，然后对其进行调度。\n\n`Go`的并发采用的是`fork-join`模型。\n\n## Go的并发哲学\nGo除了提供了goroutine之外，也提供了内存访问同步原语机制。那么在这两者之间该如何选择？回答以下四个问题：\n1. 是否尝试转移数据的所有权？\n  \u003e Yes -\u003e Channels\n\u003cbr\u003eNo  -\u003e Primitives\n2. 是否尝试保护结构体的内在状态？\n  \u003e Yes -\u003e Primitives\n\u003cbr\u003eNo  -\u003e Channels\n\n例如：\n```go\n  type Counter struct {\n    mu sync.Mutex\n    value int\n  }\n  func (c *Counter) Increment() {\n    c.mu.Lock()\n    defer c.mu.Unlock()\n    c.value++\n  }\n```\n3. 是否在多个逻辑代码中进行协调配合？\n  \u003e Yes -\u003e Channels\n\u003cbr\u003eNo  -\u003e Primitives\n4. 代码是否是性能关键区（performance-critical section） \n  \u003e Yes -\u003e Primitives\n\u003cbr\u003eNo  -\u003e Channels\n\t\t \n旨在简化，尽量使用Channel。\n\n\n# 删除临时文件\n\u003e go clean -i","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnavono%2Fgolangsimpletutorial","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnavono%2Fgolangsimpletutorial","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnavono%2Fgolangsimpletutorial/lists"}