6

golang 是单进程的吗?

 11 months ago
source link: https://www.v2ex.com/t/982738
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

V2EX  ›  Go 编程语言

golang 是单进程的吗?

  chaleaochexist · 5 小时 2 分钟前 · 2586 次点击

我在一个 goroutine 修改环境变量, 想在其他所有 goroutine 中生效? 可以吗?

56 条回复    2023-10-17 18:21:37 +08:00

xhatt      4 小时 59 分钟前

试一下不就知道了?很简单

chaleaochexist      4 小时 57 分钟前

@xhatt 我试过了 是的.但是我无法保证一定是.不知道我说没说清楚.

qloog      4 小时 53 分钟前

Golang 默认是单进程的,这与其他语言如 Java 、Node.js 有所不同。
原因有以下几点:
1. Goroutine 更轻量,一个进程可以同时运行成千上万个 Goroutine,所以不需要多进程。
2. Goroutine 有运行时(Runtime)调度和上下文切换,不需要进程或线程切换的开销。
3. 共享内存通信更简单快捷,不需要跨进程通信。
4. 不需要考虑锁、线程安全问题,编程模型更简单。
5. GC 更容易实现且高效。
但是 Golang 也可以通过 os.Exec 启动新进程,或者在不同端口监听启动多个进程。
结论:
- Golang 默认单进程,这由语言设计决定。
- 单进程可以处理更高并发,编程模型更简单。
- 也可以手动启动多进程满足特定需求。

所以 Golang 作为一种现代编程语言,单进程多 Goroutine 是更合理的选择。但也保留了启动多进程的能力。

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK