文档结构  
翻译进度:60%     翻译赏金:0 元 (?)    ¥ 我要打赏

I have written Go at Google (and in my own time) and Scala at LinkedIn. Both are modern languages with first class concurrency features.

The following answer is based on my experience writing software at scale.

Go is an opinionated, minimal language that compiles to machine code.

Scala is a sophisticated, academic, functional, object-oriented, sandbox language that has a lot of features and runs on the JVM.

For any project that needs to scale to a non-trivial number of developers, I would choose Go over Scala every time for one reason: simplicity.

第 1 段(可获 1.15 积分)

Before I dive in to my answer, I want to make a few general observations: 

  • Generally, less code is easier to understand than more code; however, there is a point at which code is so dense that it becomes hard to read.
  • Code is read much more often than it is written.
  • Code frequently lives longer than we want it to.
  • The person who tests or maintains a piece of code is frequently not the original author.
  • At scale, the skill level of developers reading/writing/maintaining/testing code is going to be a normal distribution around the mean of "not expert."

Writing code is an act of communication, not just between the author and the compiler (or runtime), but also between the author and a future reader of unknown skill level. 

Language complexity

Java 8 language spec is a 780 page PDF (ouch).
http://docs.oracle.com/javase/sp...

Scala language spec is a 191 page PDF.
http://www.scala-lang.org/docu/f...

The Go language spec is a webpage that prints as a 51 page PDF.
http://golang.org/ref/spec

Defining a language is not the same as learning how to use a language, but it is a proxy for how much there is to learn (or how much there is to confuse you when reading someone else's code).

RISC vs CISC

Go provides a small set of orthogonal primitives that interact with each other in clean expected ways. Learn a small number of primitives and build what you need with a few more lines of code compared to Scala.

Scala provides a large toolbox of types and syntax. Learn a large number of primitives, know when to use each one, and you will be able to write fewer lines of code compared to Go.

Documentation

I found Go easier to learn, both because it is (objectively) a simpler language and (subjectively) because I think the documentation is better.

Tour:
http://docs.scala-lang.org/tutor...
vs
http://tour.golang.org/#1
http://golang.org/doc/effective_...

FAQ:
http://docs.scala-lang.org/tutor...
vs
http://golang.org/doc/faq

Standard library:
http://www.scala-lang.org/api/cu...
vs
http://golang.org/pkg/

Expressiveness

Given the language specs, it should be no surprise that Scala has more features than Go.

One of Go's features is that it doesn't have an excess of features, and frankly, I think that feature is undervalued.
http://golang.org/doc/faq#Why_do...

Does that mean Go is any less expressive than Scala?
No.

Expressiveness means "effectively conveying thought or feeling"; it doesn't mean "which language has the most features". In practice, the expressiveness of code is determined more by the author than the language in use.

Code density can be counter-productive to expressiveness.

The Sim City effect

Don't bother Googling this, I just made it up. You know Sim City, right?
SimCity Official Website

If two people start a new city on the same map and play for a few hours, they will produce completely different looking cities.

Why is that?
Because Sim City is a sandbox and there are so many options that it is highly improbable that any two people will make the same series of decisions.

Scala is a sandbox too.
In addition to the plethora of features that Scala ships with (functional programming features, OO programming features, etc.), Scala exposes capabilities that allow developers to add new features.

This is all well and good until you need to share your sandbox with others and they have no idea what was going on in your head.

Code consistency

Go is the only language that I know of that has circumvented the whole code style debate by just providing a tool to format your code in the canonical format.
http://golang.org/cmd/gofmt/

Code density

Scala code can be very dense and hard to grok at the early stages of learning.

I would go so far as to say that it is idiomatic in Scala to be as dense as possible, or at least it seems to be that experienced Scala developers have a propensity for terseness.

第 2 段(可获 8.68 积分)

例如

考虑从cookie获取用户ID。 你需要多少语言知识来回答如何实现下列问题? 

  • 如果cookie不存在会发生什么?
  • 如果cookie值不是格式化好的数字会发生什么?
  • 如果cookie值是负数会发生什么?

Scala

import play.api.mvc.RequestHeader
 
def getUserId()(implicit request: RequestHeader) = {
  request.cookies.get("uid").map(_.value.toLong).filter(_ > 0)
}

Go

import (
  "fmt"
  "http"
  "strconv"
)
 
func getUserId(r *http.Request) (int64, error) {
  c, err := r.Cookie("uid")
  if err != nil {
    return 0, err
  }
  i, err := strconv.ParseInt(c.Value, 10, 64)
  if err != nil {
    return 0, err
  }
  if i <= 0 {
    return 0, fmt.Errorf("invalid user id")
  }
  return i, nil
}
第 3 段(可获 0.7 积分)

在这种特殊情况下, Scala代码很短也许更有说服力, 但有一点我想说明一般来说 Go的代码是显式的而Scala的代码需要上下文来理解。

显示的$#%!ing

根据我的经验,显式代码有很多好处。

  • 显式代码更易于让新手和非作者精神交流。
  • 显式代码更易于编辑小的变更。
  • 显式代码使错误情况更清晰。
  • 显式代码使测试用例清晰。
  • 显式代码更容易调试 (尝试在之前Scala代码中设置断点 ).
第 4 段(可获 1.28 积分)

我发现Go的代码比Scala清晰的多。

性能

作为一名开发者,性能是我在开发周期中唯一所关心的, 因为我不想遇到一个在我想做到什么的时候而不够快的运行时。无论如何开发者的时间比电脑的时间更有价值。

根据我的经验, Go编译非常块, Scala则相对慢些。因人而异。

说句公道话

我在学Scala之前先学习的Go,所以我承认我更倾向GO。我对Go第一反应是它的语法很难看 (像C++) 但是学习Go的感觉像这样:

我是出于必要学习的Scala并且已经习惯了它;我现在甚至喜欢上它的一部分, 但学习Scala是这种感觉:

第 5 段(可获 1.7 积分)

文章评论