golang编程教学_使用Go进行简单,整洁的编程

golang编程教学

GoLang (generally shortened to Go) is the latest language-that-everyone-wants-to-learn-and-use. In this article, I'll provide an introduction to the language, its features, its potential, and whether you should care about Go or not.

GoLang (通常缩写为Go )是每个人都想学习和使用的最新语言 。 在本文中,我将介绍该语言,其功能,潜力以及是否应该关心Go。

为什么要创建 (Why Go Was Created)

Go gopher

Go gopher, by Renee French

Go gopher,作者Renee French

Go is not as new as you may think. Its origins lie in 2007 with Google internal projects, until its public release in 2009. The turning point for the project was the release of version 1 in March 2012, with the aims of the project succinctly communicated by one of the project's creators in an infamous blog post.

Go并不像您想象的那么新。 它的起源于2007年与Google内部项目合作,直到2009年公开发布。该项目的转折点是2012年3月发布了版本1,该项目的目的是由一位项目的创建者以一个臭名昭著的方式进行了简单地传达博客文章

Go was created due to a frustration with existing languages suitable for large-scale and infrastructure-level programming. No option combined efficient compilation, execution and programming ease. A language choice was always a compromise on one or more of these.

Go的创建是由于对适用于大规模和基础结构级编程的现有语言感到沮丧。 没有选择结合有效的编译,执行和编程简便性。 语言选择始终是其中一个或多个的折衷方案。

The core features of Go (covered later) contribute towards its stability and efficiency, and the language was designed to be as simple and clutter-free as possible—starting from a modern outlook, instead of building upon decades of development.

Go的核心功能(稍后将介绍)有助于其稳定性和效率,并且该语言被设计为尽可能简单和整洁—从现代的观点出发,而不是基于数十年的发展。

谁在使用Go? (Who's Using Go?)

It may feel that everyone is talking about Go, but its rise in popularity is debatable. Much like Docker, the increasing need for scalable applications mean that everyone's talking about several new buzzword-laden technologies. That doesn't mean they're used extensively in production (yet). Disclaimer aside, Go is a rising star, and is attracting a lot of contributors and big-name supporters. Definitely a language to learn and appreciate, but maybe not to convert all your projects to tomorrow.

可能感觉到每个人都在谈论Go,但是它的普及程度值得商bat。 与Docker一样,对可伸缩应用程序的需求不断增长,这意味着每个人都在谈论几种载有大量流行词的新技术。 这并不意味着它们已在生产中得到广泛使用(尚未)。 除了免责声明,Go是一颗冉冉升起的新星,并吸引了许多贡献者和知名支持者。 绝对是一种可以学习和欣赏的语言,但可能不会将您的所有项目都转化为明天。

With large-scale applications, no one language dominates an entire stack, but here are some well known projects and companies that use Go in significant quantity:

在大规模应用程序中,没有一种语言可以控制整个堆栈,但是以下是一些大量使用Go的知名项目和公司:

Go的功能 (Features of Go)

Many of the reasons Go is chosen for large-scale, distributed and network-heavy applications are due to its feature set.

选择Go的原因很多,这归功于它的功能集。

简约设计 (Minimalistic Design)

Go is designed with a pragmatic, minimal feature set. A lot of complexity and extraneous concepts have been stripped away from C and re-implemented in Go. For example, the language has object-oriented concepts, but they're stripped to bare basics.

Go的设计具有实用的最小功能集。 许多复杂性和无关紧要的概念已从C中剥离出来,并在Go中重新实现。 例如,该语言具有面向对象的概念,但是它们被剥夺了基础知识。

Despite this, Go has been well planned and is capable of complex tasks—just without a lot of the coding overhead you may be used to.

尽管如此,Go还是经过了周密的计划,能够执行复杂的任务-只是没有很多您可能习惯的编码开销。

静态类型 (Statically Typed)

Languages designed for simplicity—such as JavaScript—allow you to create variables dynamically and not worry about assigning the correct type of value to these variables. The language compiler will figure out the variable types needed based on your program logic at runtime. This leads to a far simpler coding experience, but can cause execution inefficiency and error—not only because of this processing overhead, but also due to occasional miss-assignment of variables.

为简单起见而设计的语言(例如JavaScript)使您可以动态创建变量,而不必担心为这些变量分配正确的值类型。 语言编译器将在运行时根据程序逻辑找出所需的变量类型。 这会导致简单得多的编码体验,但是会导致执行效率低下和出错-不仅是因为这种处理开销,而且还因为偶发的变量分配错误。

Statically typed languages like Go force a programmer to explicitly declare variable types, and code is checked at compilation time to see if variables are used correctly. This can lead to a longer (but better) coding process, but will identify and remove potential errors earlier and increase efficiency by removing this dynamic checking overhead.

诸如Go之类的静态类型语言迫使程序员明确声明变量类型,并在编译时检查代码以查看变量是否被正确使用。 这可能会导致更长(但更好)的编码过程,但是会更早地识别并消除潜在的错误,并通过消除这种动态检查开销来提高效率。

垃圾收集 (Garbage Collection)

Garbage Collection is a form of automatic memory management for applications. Its function is to ascertain which objects in memory are no longer required by the application and reclaim the space for new objects. Other resources such as network sockets and database handles are still handled manually.

垃圾收集是一种针对应用程序的自动内存管理形式。 其功能是确定应用程序不再需要内存中的哪些对象,并回收新对象的空间。 其他资源(例如网络套接字和数据库句柄)仍然是手动处理的。

已编译 (Compiled)

Languages like PHP and JavaScript are interpreted, with instructions executed directly via an interpreter at run time. Go is a compiled language, with code first compiled into machine language before execution.

诸如PHP和JavaScript之类的语言会被解释 ,并在运行时通过解释器直接执行指令。 Go是一种编译语言,代码在执行之前首先被编译为机器语言。

A compiled language is generally faster and more efficient to run, but viewing code changes is a longer process, and all platforms your program supports will need individual and specifically compiled targets.

通常,编译后的语言运行起来更快,效率更高,但是查看代码更改是一个更长的过程,并且程序支持的所有平台都需要单独且经过专门编译的目标。

Go is renowned for its fast compilation time, bringing it almost on par with an interpreted language for development speed.

Go以其快速的编译时间而闻名,使它几乎与可解释的语言同等的开发速度。

大型标准图书馆 (Large Standard Library)

A standard library is a set of library functions available across programming languages. These functions are separate in a library for import when your code requires them. Go has a large package library, covering functionality such as compression, I/O, database access and more.

标准库是一组跨编程语言可用的库函数。 当您的代码需要这些函数时,这些函数在库中是单独的,以便导入。 Go有一个很大的程序包库 ,涵盖了压缩,I / O,数据库访问等功能。

Importing a package is a simple statement. To import the gif sub-package of the image package, you simple need this:

导入软件包很简单。 要导入image包的gif子包,您仅需要以下步骤:

import "image/gif"

To import the image package and its sub-packages, only this:

要导入image包及其子包,只需执行以下操作:

import "image"

并发 (Concurrency)

Designed to support multiple CPUs, complex I/O pipelines and distributed infrastructure, the concurrent processing features of Go are one of its banner features and where it excels beyond other languages. By using the channels concept to control access, Go removes the problems with sharing variables that are experienced in other concurrent languages.

Go的并发处理功能旨在支持多个CPU,复杂的I / O管道和分布式基础架构,是其标志性功能之一,并且在其他语言方面表现出色。 通过使用channels概念来控制访问,Go消除了共享其他并发语言中遇到的变量的问题。

At its simplest, start concurrent processes using the go keyword:

最简单的方法是,使用go关键字启动并发进程:

package main

    import "fmt"

    func PrintGreeting(greeting string) {
        fmt.Println("Hello " + greeting)
    }

    func main() {
      go PrintGreeting("World")
    }

This consists of two functions that run concurrently, the main function and the PrintGreeting function called within it. This is a basic example, and there's plenty of other functionality available for concurrent functions and communication between them.

它由两个同时运行的函数组成,即main函数和其中调用的PrintGreeting函数。 这是一个基本示例,并且还有许多其他功能可用于并发功能以及它们之间的通信。

学习围棋 (Learning Go)

The learning curve of Go will depend on your programming background. Developers with a C background will find the syntax similar, but with the addition of the features mentioned. If you come from a Web language background, it may be a harder start.

Go的学习曲线将取决于您的编程背景。 具有C背景的开发人员将发现类似的语法,但是增加了上述功能。 如果您来自网络语言背景,那可能是一个更艰难的开始。

The developers of Go have aimed to create a language that combines the simplicity of modern interpreted languages such as Ruby and JavaScript with the reliability of traditional languages like C. If you're new to static typing and concurrent, thread-based concepts, there may be an initial learning curve. However, these paradigm changes will expose you to better overall programming practice.

Go的开发人员旨在创建一种将Ruby和JavaScript等现代解释型语言的简单性与C等传统语言的可靠性相结合的语言。如果您不熟悉静态类型和并发,基于线程的概念,成为初始学习曲线。 但是,这些范例更改将使您接触到更好的整体编程实践。

学习资源 (Learning Resources)

These are some great resources to start with:

这些是开始的一些重要资源:

And if that's not enough resources for you, the Go project maintains a comprehensive list of learning resources.

而且,如果这对您来说资源不足,那么Go项目将维护学习资源的完整列表

走得更远 (Taking Go Further)

Of course, there's much more to this language than can be covered in an introductory article, but here are some more important features of Go.

当然,这种语言的功能远远超出介绍性文章所能涵盖的范围,但是这里有一些Go的重要功能。

模板化 (Templating)

No modern language would be complete without an option to create Web pages. Go's html/template package is part of the standard library, and allows for separation of application logic and layout. As a mixture of HTML and Handlebars, template files will be familiar to anyone who has used other templating systems. For example:

没有创建网页的选项,任何现代语言都是不完整的。 Go的html/template包是标准库的一部分,并允许分离应用程序逻辑和布局。 混合使用HTML和Handlebars,使用其他模板系统的任何人都将熟悉模板文件。 例如:

<h1>{{.Title}}</h1>

<p>{{printf "%s" .Body}}</p>

构架 (Frameworks)

When a new language becomes popular, people create frameworks for it to reduce repetition and boilerplate code. Most Go frameworks are for web applications and include features such as app generators, routing, MVC and ORM Support. Options include:

当一种新语言流行时,人们为它创建框架以减少重复和样板代码。 大多数Go框架都是针对Web应用程序的,并包含诸如应用程序生成器,路由,MVC和ORM支持之类的功能。 选项包括:

移动 (Mobile)

I intend to cover this topic further in a separate article, but Go has been ported to Android and iOS. Android support potentially lays the ground for a move from Java in the future, but that's pure conjecture. If you can't wait for me to finish my article, you can find more details on mobile support here.

我打算在另一篇文章中进一步讨论该主题,但是Go已移植到Android和iOS。 对Android的支持可能会为将来从Java迁移奠定基础,但这纯粹是推测。 如果您迫不及待想完成我的文章,可以在此处找到有关移动支持的更多详细信息

结论 (Conclusion)

Compared to other programming languages, Go is a new and young contender. Typically, this can lead to undeserved hype for an immature product. Thanks to careful and smart choices made by the creators of the language, Go lives up to the hype and consistently delivers on its promise.

与其他编程语言相比,Go是一个新兴的竞争者。 通常,这会导致不成熟产品的炒作。 得益于语言创造者的精心选择,Go不辜负宣传并始终如一地兑现其承诺。

Go is not suitable for every application you throw at it and will be overkill for many projects. But if what I've covered in this article appeals to you, then … get Going!

Go并不适合您扔给它的每个应用程序,并且对于许多项目来说都是过大的选择。 但是,如果我在本文中介绍的内容对您有吸引力,那么…开始吧!

Sorry, couldn't resist that pun! If you’ve used Go, what was your experience?

抱歉,无法抗拒双关语! 如果您使用过Go,您的经验是什么?

翻译自: https://www.sitepoint.com/simple-clutter-free-programming-with-go/

golang编程教学

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值