golang优点_这是Go的一些令人惊奇的优点,而您对此并不了解

golang优点

by Kirill Rogovoy

通过基里尔·罗戈沃伊

这是Go的一些令人惊奇的优点,而您对此并不了解 (Here are some amazing advantages of Go that you don’t hear much about)

In this article, I discuss why you should give Go a chance and where to start.

在本文中,我讨论了为什么您应该给Go一个机会以及从哪里开始。

Golang is a programming language you might have heard about a lot during the last couple years. Even though it was created back in 2009, it has started to gain popularity only in recent years.

Golang是一种编程语言,您在过去几年中可能听说过很多。 尽管它创建于2009年,但直到最近几年才开始流行。

This article is not about the main selling points of Go that you usually see.

本文与您通常看到的Go的主要卖点无关。

Instead, I would like to present to you some rather small but still significant features that you only get to know after you’ve decided to give Go a try.

相反,我想向您介绍一些很小但仍然很重要的功能,这些功能只有在您决定尝试使用Go之后才能了解。

These are amazing features that are not laid out on the surface, but they can save you weeks or months of work. They can also make software development more enjoyable.

这些令人惊叹的功能并未在表面上展现出来,但是可以为您节省数周或数月的工作。 它们还可以使软件开发更加愉快。

Don’t worry if Go is something new for you. This article does not require any prior experience with the language. I have included a few extra links at the bottom, in case you would like to learn a bit more.

不用担心Go对您来说是新事物。 本文不需要任何使用该语言的经验。 如果您想了解更多信息,我在底部添加了一些额外的链接。

We will go through such topics as:

我们将讨论以下主题:

  • GoDoc

    GoDoc
  • Static code analysis

    静态代码分析
  • Built-in testing and profiling framework

    内置的测试和分析框架
  • Race condition detection

    比赛条件检测
  • Learning curve

    学习曲线
  • Reflection

    反射
  • Opinionatedness

    固执己见
  • Culture

    文化

Please, note that the list doesn’t follow any particular order. It is also opinionated as hell.

请注意,该列表不遵循任何特定顺序。 人们也认为它是地狱。

GoDoc (GoDoc)

Documentation in code is taken very seriously in Go. So is simplicity.

Go中非常重视代码文档。 简单也是如此。

GoDoc is a static code analyzing tool that creates beautiful documentation pages straight out of your code. A remarkable thing about GoDoc is that it doesn’t use any extra languages, like JavaDoc, PHPDoc, or JSDoc to annotate constructions in your code. Just English.

GoDoc是一个静态代码分析工具,可以直接在您的代码中创建漂亮的文档页面。 GoDoc的一项非凡之处在于它不使用任何其他语言(例如JavaDoc,PHPDoc或JSDoc)来注释代码中的构造。 只是英语。

It uses as much information as it can get from the code to outline, structure, and format the documentation. And it has all the bells and whistles, such as cross-references, code samples, and direct links to your version control system repository.

它使用从代码中获得的尽可能多的信息来概述,结构化和格式化文档。 它具有所有的功能,例如交叉引用,代码示例以及与版本控制系统存储库的直接链接。

All you can do is to add a good old // MyFunc transforms Foo into Bar kind of comment which would be reflected in the documentation, too. You can even add code examples which are actually runnable via the web interface or locally.

您所能做的就是添加一个很好的旧// MyFunc transforms Foo into Bar类型的注释,这也将反映在文档中。 您甚至可以添加实际上可通过Web界面或本地运行的 代码示例

GoDoc is the only documentation engine for Go that is used by the whole community. This means that every library or application written in Go has the same format of documentation. In the long run, it saves you tons of time while browsing those docs.

GoDoc是整个社区使用的唯一Go的文档引擎。 这意味着用Go编写的每个库或应用程序都具有相同格式的文档。 从长远来看,它可以节省您浏览这些文档时的大量时间。

Here, for example, is the GoDoc page for my recent pet project: pullkee — GoDoc.

例如,这是我最近的宠物项目的GoDoc页面: pullkee — GoDoc

静态代码分析 (Static code analysis)

Go heavily relies on static code analysis. Examples include godoc for documentation, gofmt for code formatting, golint for code style linting, and many others.

Go很大程度上依赖于静态代码分析。 例子包括godoc的文档, gofmt代码格式化, golint代码风格掉毛,等等。

There are so many of them that there’s even an everything-included-kind-of project called gometalinter to compose them all into a single utility.

它们太多了,甚至还有一个名为gometalinter的包含所有项目的项目将它们全部组合成一个实用程序。

Those tools are commonly implemented as stand-alone command line applications and integrate easily with any coding environment.

这些工具通常被实现为独立的命令行应用程序,并且可以轻松地与任何编码环境集成。

Static code analysis isn’t actually something new to modern programming, but Go sort of brings it to the absolute. I can’t overestimate how much time it saved me. Also, it gives you a feeling of safety, as though someone is covering your back.

静态代码分析实际上并不是现代编程中的新事物,但是Go可以将其带到绝对。 我不能高估了它为我节省了多少时间。 同样,它给您一种安全的感觉,好像有人在遮挡您的背部。

It’s very easy to create your own analyzers, as Go has dedicated built-in packages for parsing and working with Go sources.

创建自己的分析器非常容易,因为Go具有专用的内置软件包,用于解析和使用Go源代码。

You can learn more from this talk: GothamGo Kickoff Meetup: Go Static Analysis Tools by Alan Donovan.

您可以从以下演讲中了解更多信息: GothamGo启动会议:Alan Donovan的Go静态分析工具

内置的测试和分析框架 (Built-in testing and profiling framework)

Have you ever tried to pick a testing framework for a Javascript project you are starting from scratch? If so, you might understand that struggle of going through such an analysis paralysis. You might have also realized that you were not using like 80% of the framework you have chosen.

您是否曾经尝试从头开始为Javascript项目选择测试框架? 如果是这样,您可能会理解经历这种分析瘫痪的挣扎。 您可能还已经意识到,所选择的框架使用的不是80%。

The issue repeats over again once you need to do some reliable profiling.

一旦需要进行可靠的性能分析,此问题就会再次发生。

Go comes with a built-in testing tool designed for simplicity and efficiency. It provides you the simplest API possible, and makes minimum assumptions. You can use it for different kinds of testing, profiling, and even to provide executable code examples.

Go带有内置的测试工具,旨在简化和提高效率。 它为您提供了最简单的API,并做了最少的假设。 您可以将其用于各种测试,性能分析,甚至提供可执行的代码示例。

It produces CI-friendly output out-of-box, and the usage is usually as easy as running go test. Of course, it also supports advanced features like running tests in parallel, marking them skipped, and many more.

它提供开箱即用的CI友好输出,使用通常与运行go test一样容易。 当然,它也支持高级功能,例如并行运行测试,将其标记为已跳过等等。

比赛条件检测 (Race condition detection)

You might already know about Goroutines, which are used in Go to achieve concurrent code execution. If you don’t, here’s a really brief explanation.

您可能已经了解Goroutine,它们在Go中用于实现并发代码执行。 如果不这样做, 这是一个非常简短的解释。

Concurrent programming in complex applications is never easy regardless of the specific technique, partly due to the possibility of race conditions.

无论采用哪种技术,在复杂应用程序中进行并发编程都不是一件容易的事,部分原因是存在竞争条件。

Simply put, race conditions happen when several concurrent operations finish in an unpredicted order. It might lead to a huge number of bugs, which are particularly hard to chase down. Ever spent a day debugging an integration test which only worked in about 80% of executions? It probably was a race condition.

简而言之,当几个并发操作以不可预测的顺序完成时,就会发生竞争条件。 它可能会导致大量错误,尤其难以追究。 您是否曾经花过一天时间调试只能在大约80%的执行中工作的集成测试? 这可能是比赛条件。

All that said, concurrent programming is taken very seriously in Go and, luckily, we have quite a powerful tool to hunt those race conditions down. It is fully integrated into Go’s toolchain.

综上所述,并发编程在Go中非常重视,而且幸运的是,我们有一个功能强大的工具可以解决这些竞争条件。 它已完全集成到Go的工具链中。

You can read more about it and learn how to use it here: Introducing the Go Race Detector — The Go Blog.

您可以在此处阅读有关它的更多信息并了解如何使用它: Go Race Detector简介— Go Blog

学习曲线 (Learning curve)

You can learn ALL Go’s language features in one evening. I mean it. Of course, there are also the standard library, and the best practices in different, more specific areas. But two hours would totally be enough time to get you confidently writing a simple HTTP server, or a command-line app.

您可以在一晚上学习ALL Go的语言功能。 我是认真的。 当然,在不同的,更具体的领域中也有标准库和最佳实践。 但是两个小时完全可以让您自信地编写一个简单的HTTP服务器或命令行应用程序。

The project has marvelous documentation, and most of the advanced topics have already been covered on their blog: The Go Programming Language Blog.

该项目有出色的文档 ,大多数高级主题已经在其博客: Go编程语言博客中进行了介绍

Go is much easier to bring to your team than Java (and the family), Javascript, Ruby, Python, or even PHP. The environment is easy to setup, and the investment your team needs to make is much smaller before they can complete your first production code.

与Java(及家族),Javascript,Ruby,Python甚至PHP相比,Go更容易带给您的团队。 该环境易于设置,并且您的团队在完成您的第一个生产代码之前需要进行的投资要少得多。

反射 (Reflection)

Code reflection is essentially an ability to sneak under the hood and access different kinds of meta-information about your language constructs, such as variables or functions.

代码反射本质上是一种潜入后台并访问有关您的语言构造的各种元信息(例如变量或函数)的能力。

Given that Go is a statically typed language, it’s exposed to a number of various limitations when it comes to more loosely typed abstract programming. Especially compared to languages like Javascript or Python.

鉴于Go是一种静态类型的语言,当涉及到更为宽松的类型的抽象编程时,它会遇到许多种种限制。 特别是与Javascript或Python之类的语言相比。

Moreover, Go doesn’t implement a concept called Generics which makes it even more challenging to work with multiple types in an abstract way. Nevertheless, many people think it’s actually beneficial for the language because of the amount of complexity Generics bring along. And I totally agree.

而且,Go 并没有实现称为泛型的概念 ,这使得以抽象方式处理多种类型的挑战更加艰巨。 然而,由于泛型带来的复杂性,许多人认为这实际上对语言是有益的。 我完全同意。

According to Go’s philosophy (which is a separate topic itself), you should try hard to not over-engineer your solutions. And this also applies to dynamically-typed programming. Stick to static types as much as possible, and use interfaces when you know exactly what sort of types you’re dealing with. Interfaces are very powerful and ubiquitous in Go.

根据Go的理念(本身是一个单独的主题),您应该努力不过度设计解决方案。 这也适用于动态类型的编程。 尽可能使用静态类型,并在确切知道要处理的类型时使用接口。 接口在Go中非常强大且无处不在。

However, there are still cases in which you can’t possibly know what sort of data you are facing. A great example is JSON. You convert all the kinds of data back and forth in your applications. Strings, buffers, all sorts of numbers, nested structs and more.

但是,在某些情况下,您可能无法知道要面对的数据类型。 JSON是一个很好的例子。 您可以在应用程序中来回转换各种数据。 字符串,缓冲区,各种数字,嵌套结构等。

In order to pull that off, you need a tool to examine all the data in runtime that acts differently depending on its type and structure. Reflection to rescue! Go has a first-class reflect package to enable your code to be as dynamic as it would be in a language like Javascript.

为了实现这一目标,您需要一个工具来检查运行时中的所有数据,这些数据的行为取决于类型和结构。 反思拯救! Go具有一流的反射包,可让您的代码像Java语言一样动态。

An important caveat is to know what price you pay for using it — and only use it when there is no simpler way.

一个重要的警告是要知道您为使用它付出了什么代价—仅在没有更简单的方法时才使用它。

You can read more about it here: The Laws of Reflection — The Go Blog.

您可以在此处阅读更多内容: 反射法则-Go Blog

You can also read some real code from the JSON package sources here: src/encoding/json/encode.go — Source Code

您还可以在此处从JSON包源中读取一些真实代码: src / encoding / json / encode.go-源代码

固执己见 (Opinionatedness)

Is there such a word, by the way?

顺便说一句,有这样一个词吗?

Coming from the Javascript world, one of the most daunting processes I faced was deciding which conventions and tools I needed to use. How should I style my code? What testing library should I use? How should I go about structure? What programming paradigms and approaches should I rely on?

来自Javascript世界,我面临的最艰巨的过程之一就是确定我需要使用哪些约定和工具。 我应该如何设置代码样式? 我应该使用哪个测试库? 我应该如何进行结构设计? 我应该依靠哪些编程范例和方法?

Which sometimes basically got me stuck. I was doing this instead of writing the code and satisfying the users.

有时候,这基本上使我陷入困境。 我这样做不是为了编写代码并使用户满意。

To begin with, I should note that I totally get where those conventions should come from. It’s always you and your team. Anyway, even a group of experienced Javascript developers can easily find themselves having most of the experience with entirely different tools and paradigms to achieve kind of the same results.

首先,我应该指出,我完全明白了这些约定的来源。 永远是您和您的团队。 无论如何,即使是一群经验丰富的Javascript开发人员,也可以通过完全不同的工具和范例轻松地获得大部分经验,从而获得相同的结果。

This makes the analysis paralysis cloud explode over the whole team, and also makes it harder for the individuals to integrate with each other.

这使得分析瘫痪云在整个团队中爆炸,并且使个人更难以彼此集成。

Well, Go is different. You have only one style guide that everyone follows. You have only one testing framework which is built into the basic toolchain. You have a lot of strong opinions on how to structure and maintain your code. How to pick names. What structuring patterns to follow. How to do concurrency better.

好吧,Go是不同的。 您只有一个样式指南,每个人都可以遵循。 您只有一个测试框架内置在基本工具链中。 您对如何组织和维护代码有很多强烈的意见。 如何挑选名字。 遵循哪些结构模式。 如何更好地进行并发。

While this might seem too restrictive, it saves tons of time for you and your team. Being somewhat limited is actually a great thing when you are coding. It gives you a more straightforward way to go when architecting new code, and makes it easier to reason about the existing one.

尽管这似乎过于严格,但可以为您和您的团队节省大量时间。 在进行编码时,一定程度上的限制实际上是一件好事。 在设计新代码时,它为您提供了一种更直接的方法,并使对现有代码的推理更加容易。

As a result, most of the Go projects look pretty alike code-wise.

结果,大多数Go项目在代码方面看起来都非常相似。

文化 (Culture)

People say that every time you learn a new spoken language, you also soak in some part of the culture of the people who speak that language. Thus, the more languages you learn, more personal changes you might experience.

人们说,每当您学习一种新的口头语言时,您也会沉迷于使用该语言的人们的某些文化中。 因此,您学习的语言越多,您可能会遇到的更多个人改变。

It’s the same with programming languages. Regardless of how you are going to apply a new programming language in the future, it always gives you a new perspective on programming in general, or on some specific techniques.

编程语言也是如此。 不管您将来如何应用新的编程语言,它始终为您提供有关一般编程或某些特定技术的新视角。

Be it functional programming, pattern matching, or prototypal inheritance. Once you’ve learned it, you carry these approaches with you which broadens the problem-solving toolset that you have as a software developer. It also changes the way you see high-quality programming in general.

无论是函数式编程,模式匹配还是原型继承。 学习完这些方法后,便可以随身携带这些方法,从而拓宽了软件开发人员所拥有的解决问题的工具集。 它也改变了您通常看高质量节目的方式。

And Go is a terrific investment here. The main pillar of Go’s culture is keeping simple, down-to-earth code without creating many redundant abstractions and putting the maintainability at the top. It’s also a part of the culture to spend the most time actually working on the codebase, instead of tinkering with the tools and the environment. Or choosing between different variations of those.

Go在这里是一笔了不起的投资。 Go的文化的主要Struts是保持简单,扎实的代码,而无需创建许多多余的抽象并将可维护性放在首位。 花费大量时间实际在代码库上工作,而不是去修改工具和环境,这也是文化的一部分。 或在不同的变体之间进行选择。

Go is also all about “there should be only one way of doing a thing.”

Go还涉及“应该只有一种做事方式”。

A little side note. It’s also partially true that Go usually gets in your way when you need to build relatively complex abstractions. Well, I’d say that’s the tradeoff for its simplicity.

一点说明。 当需要构建相对复杂的抽象时,Go通常会妨碍您的工作,这在一定程度上是正确的。 好吧,我会说这是其简单性的折衷。

If you really need to write a lot of abstract code with complex relationships, you’d be better off using languages like Java or Python. However, even when it’s not obvious, it’s very rarely the case.

如果您确实需要编写许多具有复杂关系的抽象代码,那么最好使用Java或Python之类的语言。 但是,即使不是很明显,也很少见。

Always use the best tool for the job!

始终使用最好的工具进行工作!

结论 (Conclusion)

You might have heard of Go before. Or maybe it’s something that has been staying out of your radar for a while. Either way, chances are, Go can be a very decent choice for you or your team when starting a new project or improving the existing one.

您可能以前听说过Go。 也许这是一段时间以来一直不在您的关注范围之内的事情。 无论哪种方式,在开始新项目或改进现有项目时,Go都是您或您的团队非常不错的选择。

This is not a complete list of all the amazing things about Go. Just the undervalued ones.

这不是有关Go的所有奇妙功能的完整列表。 只是被低估的

Please, give Go a try with A Tour of Go which is an incredible place to start.

请尝试一下“ Go之行” ,这是一个令人难以置信的起点。

If you wish to learn more about Go’s benefits, you can check out these links:

如果您想进一步了解Go的好处,可以查看以下链接:

Share your observations down in the comments!

在评论中分享您的意见!

Even if you are not specifically looking for a new language to use, it’s worth it to spend an hour or two getting the feel of it. And maybe it can become quite useful for you in the future.

即使您不是特别在寻找要使用的新语言,花一两个小时来感受一下它也是值得的。 也许将来它会对您变得很有用。

Always be looking for the best tools for your craft!

一直在寻找适合您Craft.io的最佳工具!

If you like this article, please consider following me for more, and clicking on those funny green little hands right below this text for sharing. ???

如果您喜欢这篇文章,请考虑关注我以获取更多信息,然后单击此文本下方的那些有趣的绿色小手以进行共享。 ???

Check out my Github and follow me on Twitter!

查看我的Github并在Twitter上关注我!

翻译自: https://www.freecodecamp.org/news/here-are-some-amazing-advantages-of-go-that-you-dont-hear-much-about-1af99de3b23a/

golang优点

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值