我以為我可以用一塊石頭殺死兩只鳥,并通過轉(zhuǎn)換以下示例代碼(取自http://blog.smartbear.com/programming/an-introduction-to-the-go-language)自學(xué) Go 和 Erlang -boldly-going-where-no-man-has-ever-gone-before/ ) 從 Go 到 Erlang:package mainimport ( "fmt" "time")type Ball struct{ hits int }func main() { table := make(chan *Ball) go player("ping", table) go player("pong", table) table <- new(Ball) // game on; toss the ball time.Sleep(1 * time.Second) <-table // game over; grab the ball}func player(name string, table chan *Ball) { for { ball := <-table ball.hits++ fmt.Println(name, ball.hits) time.Sleep(100 * time.Millisecond) table <- ball }}然而我失敗了。該代碼基本上創(chuàng)建了一個共享計(jì)數(shù)器(球)并在兩個 goroutine(玩家)之間來回發(fā)送它。到目前為止一切順利。但是我如何在 Erlang 中做類似的事情呢?我的問題是:如何在 Erlang 中創(chuàng)建計(jì)數(shù)器?Erlang 似乎不允許在設(shè)置后更改變量的值。如何在兩個 erlang 進(jìn)程之間共享這樣的計(jì)數(shù)器?
如何將此示例代碼從 Go 轉(zhuǎn)換為 Erlang
料青山看我應(yīng)如是
2021-08-23 15:52:20