golang简介_Golang简介
golang簡(jiǎn)介
This post intends to be an introduction to the Go programming language, also known as Golang.
本文旨在對(duì)Go編程語(yǔ)言(也稱(chēng)為Golang)進(jìn)行介紹。
免責(zé)聲明 (Disclaimer)
I’m not an expert in Go. In fact, I’ve started learning about Go very recently. Therefore, take everything in this post with a pinch of salt.
我不是Go方面的專(zhuān)家。 實(shí)際上,我最近才開(kāi)始學(xué)習(xí)Go。 因此,在這篇文章中的所有內(nèi)容都請(qǐng)加上少量鹽。
Then… why am I writing a post about Go? It’s simple: I want to use this post as a tool to reinforce my learning process. I believe that working on a blog post and publishing it will force me to get every detail straight. Moreover, I’ll be crafting the guide that I would have liked to have found in the first place (and, therefore, it is likely that it will become the guide that you were looking for as well). This also means that this post will probably be evolving as I learn new aspects of Go.
那……為什么我要寫(xiě)一篇關(guān)于Go的文章? 很簡(jiǎn)單:我想將這篇文章用作加強(qiáng)學(xué)習(xí)過(guò)程的工具 。 我相信,處理博客文章并發(fā)布它會(huì)迫使我弄清每個(gè)細(xì)節(jié)。 此外,我將首先編寫(xiě)本來(lái)希望找到的指南(因此,它很可能也將成為您正在尋找的指南)。 這也意味著,隨著我學(xué)習(xí)Go的新方面,這篇文章可能會(huì)不斷發(fā)展。
Keep in mind, though, that despite of the fact that I’ll be working on every detail of the post, I may make mistakes. I encourage you to indicate in the comments section any errors that you may find, and I’ll gladly correct them.
不過(guò)請(qǐng)記住,盡管我會(huì)處理帖子的每個(gè)細(xì)節(jié),但我可能會(huì)犯錯(cuò)誤。 我鼓勵(lì)您在評(píng)論部分中指出您可能會(huì)發(fā)現(xiàn)的任何錯(cuò)誤,我們將很樂(lè)意糾正它們。
If this disclaimer has let you down and you are not interested in reading this post anymore, here are some fantastic resources to get started with Go:
如果此免責(zé)聲明使您失望,并且您不再對(duì)閱讀這篇文章感興趣,則可以使用以下很棒的資源開(kāi)始使用Go:
A Tour of Go: Go’s official learning tour
圍棋之旅 :圍棋的官方學(xué)習(xí)之旅
Golang learn: a list of community-driven learning resources
Golang學(xué)習(xí) :社區(qū)驅(qū)動(dòng)的學(xué)習(xí)資源列表
FreeCodeCamp’s YT tutorial: a fantastic video of freeCodeCamp (6+ hours, no ads)
FreeCodeCamp的YouTube教程 : freeCodeCamp的精彩視頻(6個(gè)小時(shí)以上,無(wú)廣告)
If you, however, decide to stay and keep reading… welcome!
但是,如果您決定留下來(lái)繼續(xù)閱讀……歡迎光臨!
一點(diǎn)歷史 (A bit of history)
Golang was born in Google, and it was created by Robert Griesemer, Rob Pike and Ken Thompson. It was discussed for the first time on September 21, 2007 and eventually became an open-source project on November 10, 2009.
Golang出生于Google,由Robert Griesemer, Rob Pike和Ken Thompson創(chuàng)立。 它在2007年9月21日進(jìn)行了首次討論,并最終在2009年11月10日成為一個(gè)開(kāi)源項(xiàng)目。
As reported in Go’s site, this language was born due to the commonly necessary choice between an efficient compilation, efficient execution or ease of programming at the time of choosing a programming language to work with. Therefore, the main goal of Go was to create a language that would achieve the ease of an interpreted and dynamically typed language, while preserving the efficiency and safety of a statically typed, compiled language. Another one of the main goals was to take advantage of the growth of multicore CPUs, making concurrency one of the priorities of the language.
正如Go網(wǎng)站上所報(bào)道的那樣 ,之所以誕生這種語(yǔ)言,是因?yàn)樵谶x擇要使用的編程語(yǔ)言時(shí),通常需要在高效編譯,高效執(zhí)行或易于編程之間進(jìn)行選擇。 因此,Go的主要目標(biāo)是創(chuàng)建一種語(yǔ)言,該語(yǔ)言可以輕松實(shí)現(xiàn)解釋型和動(dòng)態(tài)類(lèi)型化的語(yǔ)言,同時(shí)保留靜態(tài)類(lèi)型的編譯型語(yǔ)言的效率和安全性。 另一個(gè)主要目標(biāo)是利用多核CPU的增長(zhǎng),使并發(fā)成為該語(yǔ)言的優(yōu)先考慮之一 。
To get a deeper insight into the goals of Go, I’d recommend reading this article, written by Rob Pike. Some of the goals mentioned in this article include finding a solution for:
為了更深入地了解Go的目標(biāo),我建議閱讀由Rob Pike撰寫(xiě)的本文 。 本文提到的一些目標(biāo)包括找到以下解決方案:
- slow builds 緩慢的構(gòu)建
- uncontrolled dependencies 不受控制的依賴(lài)
- each programmer using a different subset of the language 每個(gè)程序員使用語(yǔ)言的不同子集
- poor program understanding (code hard to read, poorly documented, and so on) 對(duì)程序的理解較差(代碼難于閱讀,文檔記錄不良等)
- duplication of effort 重復(fù)勞動(dòng)
- cost of updates 更新費(fèi)用
- version skew 版本偏斜
- difficulty of writing automatic tools 編寫(xiě)自動(dòng)工具的難度
- cross-language builds 跨語(yǔ)言構(gòu)建
Considering that Google works with large-scale systems that have huge codebases, Go was primarily developed as a language “in the service of software engineering”, attempting to solve the most common issues in large systems.
考慮到Google可以與具有大量代碼庫(kù)的大規(guī)模系統(tǒng)一起使用,Go最初是作為一種“為軟件工程服務(wù)”的語(yǔ)言開(kāi)發(fā)的,旨在解決大型系統(tǒng)中最常見(jiàn)的問(wèn)題。
去的吉祥物 (Go’s mascot)
If you are going to start programming in Go, you need to know the language’s mascot! It’s called the gopher.
如果要開(kāi)始使用Go編程,則需要了解該語(yǔ)言的吉祥物! 它被稱(chēng)為地鼠。
Image source] 圖片來(lái)源 ]Learn about the gopher here!
在這里了解地鼠!
Go入門(mén) (Getting started with Go)
建立我們的開(kāi)發(fā)環(huán)境 (Setting up our development environment)
In order to follow through this post, you have two options:
為了繼續(xù)閱讀本文,您有兩種選擇:
Use Go’s official online playground
使用Go的官方在線游樂(lè)場(chǎng)
I will not go into the installation details for each platform, but here are some guides:
我不會(huì)介紹每個(gè)平臺(tái)的安裝詳細(xì)信息,但以下是一些指南:
Windows 10
Windows 10
macOS
蘋(píng)果系統(tǒng)
Ubuntu 18.04
Ubuntu 18.04
If you, however, prefer not to commit for now and test out the language, option 2 is a perfectly valid option. In any case, I recommend you to go through this introduction executing the examples, instead of just reading the code: I’m sure you would get a better grasp of the language.
但是,如果您不想立即提交并測(cè)試語(yǔ)言,則選項(xiàng)2是一個(gè)非常有效的選項(xiàng)。 無(wú)論如何,我建議您通過(guò)執(zhí)行本示例來(lái)完成本簡(jiǎn)介,而不僅僅是閱讀代碼:我相信您會(huì)更好地理解該語(yǔ)言。
你好,世界! (Hello world!)
Well, first things first! Let’s get started writing our first program in Go. In the following sections I’ll be explaining in more detail each part of our program. We may print our “Hello world!” message with the following few lines of code:
好吧,第一件事首先! 讓我們開(kāi)始用Go編寫(xiě)我們的第一個(gè)程序。 在以下各節(jié)中,我將更詳細(xì)地說(shuō)明程序的每個(gè)部分。 我們可以打印“ Hello world!” 帶有以下幾行代碼的消息:
package mainimport "fmt"func main() {fmt.Println("Hello world!")
}
We’ll save the above snippet in a file called Main.go and then run the following instruction in our CLI:
我們將上面的代碼段保存在一個(gè)名為Main.go的文件中,然后在CLI中運(yùn)行以下指令:
go run Main.goVoilà! If everything worked correctly, you should see our message printed in the terminal. Congrats! You’ve written your first Go program!
瞧! 如果一切正常,您應(yīng)該在終端上看到我們的消息。 恭喜! 您已經(jīng)編寫(xiě)了第一個(gè)Go程序!
Let’s make an initial explanation of the above snippet, so that we start getting our first building blocks. Go programs are organized into packages, and one special package is “main”, which must be used in executable commands. We may now move towards the import statement, which allows us to include packages in our program. It may be worth mentioning that we may import multiple packages with the following syntax:
讓我們對(duì)上面的代碼片段做一個(gè)初步的解釋,以便我們開(kāi)始獲得我們的第一個(gè)構(gòu)建塊。 Go程序被組織成軟件包 ,其中一個(gè)特殊的軟件包是“ main”,必須在可執(zhí)行命令中使用。 現(xiàn)在,我們可以轉(zhuǎn)向import語(yǔ)句,該語(yǔ)句允許我們?cè)诔绦蛑邪绦虬?值得一提的是,我們可以使用以下語(yǔ)法導(dǎo)入多個(gè)軟件包:
import ("<package_1>"
"<package_2>"
)
In our example, we can see the “fmt” package, which is commonly used to read from stdin or write to stdout. We’ll be discovering this package in more detail as this article develops.
在我們的示例中,我們可以看到“ fmt”包 ,該包通常用于從stdin讀取或?qū)懭雜tdout。 隨著本文的發(fā)展,我們將更詳細(xì)地發(fā)現(xiàn)此軟件包。
Finally, we may find the func main() declaration which, unsurprisingly, declares the main function of our Go program.
最后,我們可能會(huì)發(fā)現(xiàn)func main()聲明,毫不奇怪,該聲明聲明了Go程序的main函數(shù)。
基礎(chǔ) (The basics)
聲明和初始化變量 (Declaring and initializing variables)
Go is a statically typed language. This means that, unlike dynamically typed languages (such as JavaScript or Python), you initially declare the type of a variable and stick to it. For instance, if you declare an integer variable, it cannot become a string in a later stage of its lifecycle. Even though it becomes more verbose and strict, it gives the compiler more opportunities to optimize your code, given that the variable type is always the same. Check out this article for a more in-depth comparison of statically and dynamically typed languages.
Go是一種靜態(tài)類(lèi)型的語(yǔ)言。 這意味著,與動(dòng)態(tài)類(lèi)型的語(yǔ)言(例如JavaScript或Python)不同,您首先聲明變量的類(lèi)型并堅(jiān)持使用。 例如,如果聲明一個(gè)整數(shù)變量,則它在其生命周期的后期將無(wú)法成為字符串。 盡管它變得更加冗長(zhǎng)和嚴(yán)格,但鑒于變量類(lèi)型始終相同,它為編譯器提供了更多優(yōu)化代碼的機(jī)會(huì)。 請(qǐng)查看本文 ,以更深入地比較靜態(tài)和動(dòng)態(tài)類(lèi)型的語(yǔ)言。
Let’s declare our first variables. To do that, we’ll have to use the following syntax:
讓我們聲明我們的第一個(gè)變量。 為此,我們必須使用以下語(yǔ)法:
var <name> <type> [= <value>]I’m using the values between < and > as placeholders, and the square brackets [ ] to indicate an optional part of the statement. Therefore, both of the following statements would be valid:
我將<和>之間的值用作占位符,并使用方括號(hào)[]表示該語(yǔ)句的可選部分。 因此,以下兩個(gè)語(yǔ)句都是有效的:
var i intvar j string = "hello"
Note that, while we have explicitly initialized variable j with the string "hello", the variable i is actually initialized as well: declared-only variables are initialized with a "zero value", which translates into 0 for numbers, "" for strings and false for boolean variables.
請(qǐng)注意,盡管我們已經(jīng)使用字符串“ hello”顯式初始化了變量j ,但變量i實(shí)際上也被初始化了:僅聲明的變量使用“零值”初始化,數(shù)字表示為0,字符串表示為“”。布爾變量為false。
You may, moreover, declare (and optionally initialize) multiple variables of the same type using a single line of code with the following syntax:
此外,您可以使用單行代碼使用以下語(yǔ)法聲明(并可選地初始化)相同類(lèi)型的多個(gè)變量:
var "name_1", "name_2" "type" [= "val_1", "val_2"]Let’s illustrate this feature with an example:
讓我們用一個(gè)例子來(lái)說(shuō)明這個(gè)功能:
var i, j int = 12, 27Golang, however, not only had the goal of producing efficient code, but also to make the process of programming faster. This is why we may also initialize a variable using the walrus operator :=. Using this operator, the syntax becomes more succinct, and we do not need to explicitly define the variable type (Go decides it for us, aka type inference). It's important to note, though, that the walrus operator may only be used inside a function. The following example may illustrate how simple it becomes to declare and initialize a variable:
但是,Golang的目標(biāo)不僅是生成高效的代碼,而且還在于使編程過(guò)程更快。 這就是為什么我們也可以使用walrus運(yùn)算符:=初始化變量的原因。 使用此運(yùn)算符,語(yǔ)法變得更加簡(jiǎn)潔,我們不需要顯式定義變量類(lèi)型(Go會(huì)為我們確定變量類(lèi)型,也就是類(lèi)型推斷)。 不過(guò),請(qǐng)務(wù)必注意,海象運(yùn)算符只能在函數(shù)內(nèi)部使用。 下面的示例可以說(shuō)明聲明和初始化變量變得多么簡(jiǎn)單:
k := 42.0Go, moreover, allows you to decide the visibility of a variable based on its name. If your variable starts with a lowercase letter, it will remain internal to the package. However, if the first letter is uppercase, it will become visible (or exported) to other packages.
此外,Go允許您根據(jù)變量的名稱(chēng)來(lái)決定其可見(jiàn)性。 如果您的變量以小寫(xiě)字母開(kāi)頭,它將保留在包的內(nèi)部。 但是,如果第一個(gè)字母是大寫(xiě)字母,它將在其他包中可見(jiàn)(或?qū)С?。
I’d like to conclude this section by adding the following caveats:
我想通過(guò)添加以下警告來(lái)結(jié)束本節(jié):
- all variables need to be used, otherwise Go will return a compile-time error. 所有變量都需要使用,否則Go將返回編譯時(shí)錯(cuò)誤。
redeclaring a variable is not allowed (with the exception of shadowing).
不允許重新聲明變量( 陰影除外)。
- the above walrus operator example will create a variable of type float64. If we want to create a float32, we need to use the more verbose declaration. 上面的海象運(yùn)算符示例將創(chuàng)建一個(gè)float64類(lèi)型的變量。 如果要?jiǎng)?chuàng)建float32,則需要使用更詳細(xì)的聲明。
You may also be interested in checking out the list of allowed data types.
您可能還想查看允許的數(shù)據(jù)類(lèi)型列表 。
在類(lèi)型之間轉(zhuǎn)換 (Converting between types)
We may convert (or cast) a value into a different type by applying the following syntax:
我們可以通過(guò)應(yīng)用以下語(yǔ)法將值轉(zhuǎn)換(或強(qiáng)制轉(zhuǎn)換)為其他類(lèi)型:
<type>(<var>)Naturally, in-place casting is not allowed, as we are working in a statically typed language:
當(dāng)然,由于我們使用的是靜態(tài)類(lèi)型的語(yǔ)言,因此不允許就地轉(zhuǎn)換:
var i int = 12var j float32 = float32(i)
Let’s now attempt to convert an integer into a string:
現(xiàn)在讓我們嘗試將整數(shù)轉(zhuǎn)換為字符串:
a := 65b := string(a)
fmt.Println(b)
If you run the above 3 lines of code, you’ll see that it prints out “A”, instead of “65” as we could have expected. This happens because the direct casting of an integer into a string returns the Unicode character of the given number (“A” is the character #65 in Unicode).
如果運(yùn)行上面的三行代碼,您會(huì)看到它打印出“ A”,而不是我們預(yù)期的“ 65”。 發(fā)生這種情況是因?yàn)閷⒄麛?shù)直接轉(zhuǎn)換為字符串會(huì)返回給定數(shù)字的Unicode字符(“ A”是Unicode中的字符#65)。
So… how can we cast an integer into a string? We’ll need the strconv package for this purpose. Specifically, in this case we’ll need the Itoa (Integer to ASCII) function:
那么……我們?nèi)绾螌⒄麛?shù)轉(zhuǎn)換為字符串? 為此,我們需要strconv軟件包 。 具體來(lái)說(shuō),在這種情況下,我們需要Itoa(整數(shù)到ASCII)函數(shù) :
package mainimport ("fmt"
"strconv"
)func main() {
a := 65
b := strconv.Itoa(a)
fmt.Printf("%v, %T\n", b, b)
}
If you run the above snippet, you’ll see that b is now "65".
如果運(yùn)行上面的代碼段,您將看到b現(xiàn)在為“ 65”。
常數(shù) (Constants)
You may also define constant values using the const keyword:
您也可以使用const關(guān)鍵字定義常量值:
const z = 123Now, variable z will have a fixed value of 123 and you will not be able to modify it. I'd also like to add that the walrus operator is not applicable to constants.
現(xiàn)在,變量z的固定值為123,您將無(wú)法對(duì)其進(jìn)行修改。 我還想補(bǔ)充一下,海象運(yùn)算符不適用于常量。
注釋 (Comments)
Writing comments in your code can help you explain to your co-workers (and your future self) a fragment of code. You may write a single-line comment with the following syntax:
在代碼中編寫(xiě)注釋可以幫助您向同事(以及將來(lái)的自己)解釋代碼片段。 您可以使用以下語(yǔ)法編寫(xiě)單行注釋:
// <comment>… or multi-line comments just like this:
…或類(lèi)似這樣的多行注釋:
/*<comment>
*/
指針 (Pointers)
Go allows you to work with pointers, which hold the memory address of a value. We may define a pointer with the following syntax:
Go使您可以使用保存值的??內(nèi)存地址的指針。 我們可以使用以下語(yǔ)法定義一個(gè)指針:
var <pointer_name> *<type>As an example:
舉個(gè)例子:
var i *intWe may also use the & operator, which returns the pointer of the referenced variable:
我們還可以使用&運(yùn)算符,該運(yùn)算符返回引用變量的指針:
var <pointer_name> *<type> = &<variable>Finally, we may access the content of a pointer with the * operator:
最后,我們可以使用*運(yùn)算符訪問(wèn)指針的內(nèi)容:
*<pointer_name>To wrap up, let’s see how we may work with pointers:
總結(jié)一下,讓我們看看如何使用指針:
package mainimport "fmt"func main() {var i int = 27
var p *int = &i
fmt.Println(*p)
*p += 1
fmt.Println(i)
}
In this example, we’ve created an integer variable i set to 27 and then a pointer p referencing to it. Then, we've printed out the value of the variable through the pointer. Finally, we've incremented the value of the variable through the pointer and printed out the final value.
在此示例中,我們創(chuàng)建了一個(gè)整數(shù)變量i將其設(shè)置為27,然后創(chuàng)建了一個(gè)引用它的指針p 。 然后,我們通過(guò)指針打印出了變量的值。 最后,我們通過(guò)指針增加了變量的值并打印出最終值。
功能 (Functions)
We may create a function in Go using the following syntax:
我們可以使用以下語(yǔ)法在Go中創(chuàng)建一個(gè)函數(shù):
func <name>([<var_1> <type_1>, <var_2> <type_2>]) [<return_type>] {}In the above fragment of pseudo-code I’ve included two arguments as an example, but we may provide zero or more arguments to a function. Let’s see an example:
在上面的偽代碼片段中,我以?xún)蓚€(gè)參數(shù)為例,但是我們可以為函數(shù)提供零個(gè)或多個(gè)參數(shù)。 讓我們來(lái)看一個(gè)例子:
func multiply(i int, j int) int {return i * j
}
Note that the returned value is of the same type as the declared returned value in the function’s signature. We may now call this function as follows:
請(qǐng)注意,返回值的類(lèi)型與函數(shù)簽名中聲明的返回值的類(lèi)型相同。 現(xiàn)在,我們可以按以下方式調(diào)用此函數(shù):
multiply(3, 11)Go offers a more compact syntax when two (or more) consecutive arguments share the same type. We may simply declare the type of these variables by declaring it in the last one:
當(dāng)兩個(gè)(或多個(gè))連續(xù)參數(shù)共享同一類(lèi)型時(shí),Go提供了更緊湊的語(yǔ)法。 我們可以通過(guò)在最后一個(gè)變量中聲明它們來(lái)簡(jiǎn)單地聲明這些變量的類(lèi)型:
func multiply(i, j int) int {return i * j
}
In the above example, both arguments are of type int.
在上面的示例中,兩個(gè)參數(shù)均為int類(lèi)型。
We may also return multiple values in a function:
我們還可以在函數(shù)中返回多個(gè)值:
func personal_info() (string, int) {name := "John"
age := 27
return name, age
}
… or return named values (aka naked return):
…或返回命名值(又名裸返回):
func ops(x, y float64) (mult, div float64) {mult = x * y
div = x / y
return
}
In this case, we don’t even need to specify the variables in the return statement, since they are already specified in the function’s signature. Moreover, note that we do not need to declare these variables in the function’s body, as they are declared in the function’s signature!
在這種情況下,我們甚至不需要在return語(yǔ)句中指定變量,因?yàn)樗鼈円呀?jīng)在函數(shù)的簽名中指定了。 此外,請(qǐng)注意,我們不需要在函數(shù)的主體中聲明這些變量,因?yàn)樗鼈兪窃诤瘮?shù)的簽名中聲明的!
控制流 (Control flow)
條件語(yǔ)句 (Conditional statements)
If(-else) statements may be implemented in Go with the following syntax:
If(-else)語(yǔ)句可以使用以下語(yǔ)法在Go中實(shí)現(xiàn):
if [<statement>;] <condition> {<if-block>
} [else if <condition> {
<else-if-block>
}] [else {
<else-block>
}]
Let’s get started with the simplest case: a simple conditional statement.
讓我們從最簡(jiǎn)單的情況開(kāi)始:簡(jiǎn)單的條件語(yǔ)句。
package mainimport "fmt"var x int = 8func main() {if x >= 5 {
fmt.Println("passed")
}
}
Easy, right? Note that parentheses are not needed!
容易吧? 請(qǐng)注意,不需要括號(hào)!
Let’s spice it up a bit more:
讓我們加一點(diǎn)香料:
package mainimport "fmt"var foo int = 4var bar int = 3func main() {
if baz := foo + bar; baz < 3 {
fmt.Println("very low")
} else if baz < 5 {
fmt.Println("low")
} else if baz < 7 {
fmt.Println("medium")
} else {
fmt.Println("high")
}
}
Note the statement that defines the variable baz in the first if statement baz := foo + bar. The scope of this variable is limited to the if(-else) block.
注意在第一個(gè)if語(yǔ)句baz := foo + bar中定義變量baz的語(yǔ)句。 此變量的范圍限于if(-else)塊。
對(duì)于循環(huán) (For loops)
A loop in Go may be created with the following syntax:
可以使用以下語(yǔ)法在Go中創(chuàng)建循環(huán):
for [<initializer>;] [<condition>] [; <update>] {<loop-block>
}
Note that parentheses are not needed in Go’s for loop. Also, you may notice that both the initializer and the update step are optional, allowing you to define a for loop with only a condition. This kind of loop is normally known as a while loop in many languages. However, Go allows you to do this with a for loop.
請(qǐng)注意,在Go's for循環(huán)中不需要括號(hào)。 另外,您可能會(huì)注意到初始化程序和更新步驟都是可選的,從而使您可以?xún)H使用條件來(lái)定義for循環(huán)。 在許多語(yǔ)言中,這種循環(huán)通常稱(chēng)為while循環(huán)。 但是,Go允許您使用for循環(huán)執(zhí)行此操作。
Let’s see an example of a normal for loop:
讓我們看一個(gè)普通的for循環(huán)的例子:
package mainimport "fmt"func main() {res := 1
base := 2
exp := 9
for i := 1; i <= exp; i++ {
res = res * base
}
fmt.Println(res)
}
In this example, we’ve implemented the exponentiation (just as an example, you may use math.Pow if you need to use it).
在此示例中,我們實(shí)現(xiàn)了冪運(yùn)算(僅作為示例,您可以使用math.Pow,如果需要使用它)。
Let’s now look at a while example (no initializer and no update step):
現(xiàn)在讓我們看while示例(沒(méi)有初始化程序,沒(méi)有更新步驟):
package mainimport "fmt"func main() {i := 1
sum := 0
for i < 10 {
sum += i
i += 1
}
fmt.Println(sum)
}
This loop keeps track of an incrementing i variable, which is compared to 10 in the condition, and adds it to a sum variable. As a result, we obtain the sum of integers from 1 to 9.
此循環(huán)跟蹤遞增的i變量,該變量在條件下與10進(jìn)行比較,并將其添加到sum變量中。 結(jié)果,我們獲得了1到9之間的整數(shù)之和。
As a final note, you may ignore the condition as well, and create an infinite loop.
最后一點(diǎn),您也可以忽略條件,并創(chuàng)建一個(gè)無(wú)限循環(huán)。
推遲,恐慌和恢復(fù) (Defer, Panic and Recover)
延期 (Defer)
Defers are a very interesting instruction of Go: they allow you to execute a statement after the surrounding function has returned. They are, however, evaluated immediately.
延遲是Go的一個(gè)非常有趣的指令:延遲使您可以在周?chē)暮瘮?shù)返回后執(zhí)行一條語(yǔ)句。 但是,將立即對(duì)其進(jìn)行評(píng)估。
Let’s look at an example:
讓我們看一個(gè)例子:
package mainimport "fmt"func main() {defer fmt.Println("3")
defer fmt.Println("2") fmt.Println("1")
}
Note that I’ve included 2 defer statements. I wanted you to note that defers are stacked in a LIFO (Last In First Out) basis. In fact, if you execute the above snippet, you’ll see that the numbers are printed out in ascending order.
請(qǐng)注意,我包含了2個(gè)defer語(yǔ)句。 我想請(qǐng)您注意,延遲是按照LIFO ( 后進(jìn)先出)的基礎(chǔ)堆疊的。 實(shí)際上,如果執(zhí)行上面的代碼片段,您會(huì)看到數(shù)字按升序打印。
恐慌 (Panic)
Panic is a built-in function that allows us to indicate that something has gone wrong. It will stop the execution of the program and start unrolling the call stack, propagating the error. Let’s see how we can panic in Go:
Panic是一個(gè)內(nèi)置函數(shù),可讓我們指示出問(wèn)題了。 它將停止程序的執(zhí)行并開(kāi)始展開(kāi)調(diào)用堆棧,從而傳播錯(cuò)誤。 讓我們看看如何在Go中驚慌:
package mainimport "fmt"func main() {r := divide(1, 0)
fmt.Println(r)
}func divide(i int, j int) int {
if j == 0 {
panic("dividing by 0 is not allowed")
}
return i / j
}
As you can see, Go exits with an error status, displaying our message.
如您所見(jiàn),Go退出并顯示錯(cuò)誤狀態(tài),顯示我們的消息。
恢復(fù) (Recover)
Sometimes we’ll want to handle these errors, and we can do that by using the recover built-in function. It should be noted that this function will only have an effect if it is inside a deferred function. Let’s modify our panic example, and recover from the situation:
有時(shí)我們想處理這些錯(cuò)誤,我們可以通過(guò)使用內(nèi)置的restore函數(shù)來(lái)實(shí)現(xiàn)。 應(yīng)該注意的是,該函數(shù)只有在延遲函數(shù)內(nèi)部時(shí)才起作用。 讓我們修改我們的緊急示例,并從情況中恢復(fù):
package mainimport "fmt"func main() {defer func() {
if r := recover(); r != nil {
fmt.Println("Recovered. Let's not panic. Error received:", r)
}
}()
r := divide(1, 0)
fmt.Println(r)
}func divide(i int, j int) int {
if j == 0 {
panic("dividing by 0 is not allowed")
}
return i / j
}
In this example, the error has been handled and we are not exiting with an error status anymore. Moreover, we are printing the error message.
在此示例中,錯(cuò)誤已得到處理,并且我們不再以錯(cuò)誤狀態(tài)退出。 此外,我們正在打印錯(cuò)誤消息。
數(shù)據(jù)結(jié)構(gòu) (Data structures)
數(shù)組 (Arrays)
We may define an array in Go with the following syntax:
我們可以使用以下語(yǔ)法在Go中定義一個(gè)數(shù)組:
var <name> [<length>]<type>NB! In this case, with the square brackets [] I don’t mean an optional part… I mean square brackets.
注意! 在這種情況下,方括號(hào)[]不是指可選部分…而是指方括號(hào)。
Let’s look at an example:
讓我們看一個(gè)例子:
package mainimport "fmt"func main() {var position [2]float64
position[0] = 40.4168
position[1] = 3.7038
fmt.Println(position)
position[0] = 51.5074
fmt.Println(position)
}
Note that we may access (and update) an array index using the square brackets. It’s also important to note that Go’s arrays cannot be resized, and hence will always keep the initially set length.
請(qǐng)注意,我們可以使用方括號(hào)訪問(wèn)(并更新)數(shù)組索引。 同樣重要的是要注意,Go的數(shù)組無(wú)法調(diào)整大小,因此將始終保持初始設(shè)置的長(zhǎng)度。
An alternative syntax using the walrus operator allows to declare and initialize the array in one single statement:
使用walrus運(yùn)算符的另一種語(yǔ)法允許在一個(gè)語(yǔ)句中聲明和初始化數(shù)組:
<name> := [<length>]<type>{<value_1>, <value_2>, ..., <value_n>}For instance:
例如:
position := [2]float64{40.4168, 3.7038}切片 (Slices)
Even though arrays have a fixed length, slices allow us to work with dynamically-sized data. More specifically, they allow us to work with a dynamic range of an array. A slice may be defined with the following syntax:
即使數(shù)組具有固定的長(zhǎng)度,切片也允許我們處理動(dòng)態(tài)大小的數(shù)據(jù)。 更具體地說(shuō),它們使我們能夠處理數(shù)組的動(dòng)態(tài)范圍。 可以使用以下語(yǔ)法定義切片:
[]<type>Then, we may obtain the values of the array in a range of indices by using the following syntax:
然后,我們可以使用以下語(yǔ)法獲取索引范圍內(nèi)的數(shù)組值:
<array_name>[<min_idx] : <max_idx>]In this case, we would obtain the values of the array <array_name>, starting in index <min_idx> (included) and ending in index <max_idx> (excluded).
在這種情況下,我們將獲取數(shù)組<array_name>的值,該值從索引<min_idx> (包括)開(kāi)始,到索引<max_idx> (排除)結(jié)束。
Note again that, in these two cases above, the square brackets actually refer to… square brackets.
再次注意,在上述兩種情況下,方括號(hào)實(shí)際上是指…方括號(hào)。
Let’s look at an example to better understand this concept:
讓我們看一個(gè)例子來(lái)更好地理解這個(gè)概念:
package mainimport "fmt"func main() {vowels := [5]string{"A", "E", "I", "O", "U"} var some_vowels []string = vowels[1:3]
fmt.Println(some_vowels)
}
In this script we have an array containing the vowels and, using a slice, we print the second and third vowels.
在此腳本中,我們有一個(gè)包含元音的數(shù)組,并使用切片來(lái)打印第二個(gè)和第三個(gè)元音。
You may also omit <min_idx> and/or <max_idx> in your slices. If you don't specify them, <min_idx> will default to 0 (start of the array) and <max_idx> will default to the array length (end of the array). Let's look at this with an example:
您也可以在切片中省略<min_idx>和/或<max_idx> 。 如果未指定它們,則<min_idx>將默認(rèn)為0(數(shù)組的開(kāi)始),而<max_idx>將默認(rèn)為數(shù)組長(zhǎng)度(數(shù)組的結(jié)尾)。 讓我們來(lái)看一個(gè)例子:
package mainimport "fmt"func main() {vowels := [5]string{"A", "E", "I", "O", "U"} var some_vowels []string = vowels[:3]
fmt.Println(some_vowels) some_vowels = vowels[2:]
fmt.Println(some_vowels) some_vowels = vowels[:]
fmt.Println(some_vowels)
}
In the first example, we’ve omitted <min_idx> and displayed every vowel until index 3 (excluded). In the second one, we've omitted <max_idx> and displayed every vowel starting from index 2 (included). Finally, in the third example we've omitted both <min_idx> and <max_idx> and displayed the full array of vowels.
在第一個(gè)示例中,我們省略了<min_idx>并顯示每個(gè)元音,直到索引3(不包括)。 在第二個(gè)中,我們省略了<max_idx>并顯示從索引2(包括)開(kāi)始的每個(gè)元音。 最后,在第三個(gè)示例中,我們省略了<min_idx>和<max_idx>并顯示了完整的元音數(shù)組。
Slices can be seen as a reference to the array given that, if we modify some value in a slice, such change will reflect in the original array (and in any slices referring to the modified array position). Let’s look at this with an example:
可以將切片視為對(duì)數(shù)組的引用,因?yàn)槿绻覀冃薷那衅械哪承┲?#xff0c;則此類(lèi)更改將反映在原始數(shù)組中(以及任何引用修改后的數(shù)組位置的切片中)。 讓我們來(lái)看一個(gè)例子:
package mainimport "fmt"func main() {numbers := [5]int{1, 2, 3, 4, 5} var some_numbers []int = numbers[:3]
fmt.Println(some_numbers) var other_numbers []int = numbers[2:]
fmt.Println(other_numbers) some_numbers[2] = 7 fmt.Println(some_numbers)
fmt.Println(other_numbers)
fmt.Println(numbers)
}
As you can see, number 3 has been replaced by a 7, no matter if you print the original array or a slice of it.
如您所見(jiàn),無(wú)論您打印原始數(shù)組還是數(shù)組的一部分,數(shù)字3都已由7代替。
地圖 (Maps)
A map is a key-value data structure, aka dictionary. We may create a map either using the make function or initializing it with some initial values.
映射是鍵值數(shù)據(jù)結(jié)構(gòu),也稱(chēng)為字典。 我們可以使用make函數(shù)創(chuàng)建地圖,也可以使用一些初始值對(duì)其進(jìn)行初始化。
Let’s see how we may create it with the make function, which receives the type of the keys and the values, and creates an empty map:
讓我們看看如何使用make函數(shù)創(chuàng)建它,該函數(shù)接收鍵的類(lèi)型和值,并創(chuàng)建一個(gè)空的映射:
package mainimport "fmt"var colors map[string]stringfunc main() {colors = make(map[string]string)
colors["red"] = "#FF0000"
colors["green"] = "#00FF00"
colors["blue"] = "#0000FF" fmt.Println(colors["red"])
}
You may also create a map without the make function, if you can provide some initial values:
如果可以提供一些初始值,也可以創(chuàng)建不帶make函數(shù)的映射:
package mainimport "fmt"var colors = map[string]string {"red": "#FF0000",
"green": "#00FF00",
"blue": "#0000FF",
}func main() {
fmt.Println(colors["red"])
}
Let’s now manage the map and see how we may add an element, delete it or check whether it is present:
現(xiàn)在讓我們管理地圖,看看如何添加元素,刪除元素或檢查元素是否存在:
package mainimport "fmt"var colors = map[string]string {"red": "#FF0000",
"green": "#00FF00",
"blue": "#0000FF",
}func main() {
// add a new entry to the map
colors["black"] = "#000001" // edit an existing entry of the map
colors["black"] = "#000000" // delete an entry from the map
delete(colors, "red") fmt.Println(colors) rgb_black, present_black := colors["black"]
fmt.Printf("%s, %t\n", rgb_black, present_black) // remember that we've deleted "red"!
rgb_red, present_red := colors["red"]
fmt.Printf("%s, %t", rgb_red, present_red)
}
As you may have noticed, you can check whether a key is present in a map by unpacking the map[key] syntax to two values: the first one holds the value of the key (if present, otherwise it holds the type's zero-value), while the second value is a boolean that indicates whether it is present or not.
您可能已經(jīng)注意到,可以通過(guò)將map[key]語(yǔ)法解壓縮為兩個(gè)值來(lái)檢查映射中是否存在鍵:第一個(gè)保存鍵的值(如果存在,否則保存類(lèi)型的零值) ),而第二個(gè)值是一個(gè)布爾值,指示它是否存在。
結(jié)構(gòu) (Structs)
Go allows us to create structs, which are collections of fields. We may define a struct with the following syntax:
Go允許我們創(chuàng)建結(jié)構(gòu),這些結(jié)構(gòu)是字段的集合。 我們可以使用以下語(yǔ)法定義結(jié)構(gòu):
type <name> struct {<name_1> <type_1>
<name_2> <type_2>
...
<name_n> <type_n>
}
As an example:
舉個(gè)例子:
type Position struct {Latitude float64
Longitude float64
}
Let’s now see how we may work with this new struct:
現(xiàn)在讓我們看看如何使用這個(gè)新結(jié)構(gòu):
package mainimport "fmt"type Position struct {Latitude float64
Longitude float64
}func main() {
pos := Position{40.4168, 3.7038}
fmt.Println(pos)
pos.Latitude = 51.5074
fmt.Println(pos)
pos2 := Position{Longitude: 41.9028, Latitude: 12.4964}
fmt.Println(pos2)
}
Note how we’ve created a new instance of our struct:
注意我們?nèi)绾蝿?chuàng)建結(jié)構(gòu)的新實(shí)例:
<name> := <struct>{<value_1>, <value_2>, ..., <value_n>}Moreover, we may access (and update) the value of a field using the dot operator:
此外,我們可以使用點(diǎn)運(yùn)算符訪問(wèn)(和更新)字段的值:
<struct>.<field> [= <value>]Finally, note as well that we may define the value of our fields by using their name in the instantiation statement, even with a different order.
最后,還要注意,我們可以通過(guò)在實(shí)例化語(yǔ)句中使用它們的名稱(chēng)來(lái)定義字段的值,即使順序不同。
方法 (Methods)
Even though Go does not have the concept of classes, it does allow us to define methods. What is a method? It’s a function that is applied to a type. To better understand how to create a method in Go, we need to introduce the concept of “receiver”. Such “receiver” is the type to which this function will be applied. Let’s take a look at the syntax:
即使Go沒(méi)有類(lèi)的概念,它也允許我們定義方法。 什么是方法? 這是應(yīng)用于類(lèi)型的函數(shù)。 為了更好地理解如何在Go中創(chuàng)建方法,我們需要引入“接收器”的概念。 這種“接收器”就是將要應(yīng)用此功能的類(lèi)型。 讓我們看一下語(yǔ)法:
func (<receiver_instance> <receiver_type>) <method_name>([<arg_1> <type_1>, <arg_2> <type_2>, ..., <arg_n> <type_n>]) [<return_type>] {<method_body>
}
Let’s look at an example:
讓我們看一個(gè)例子:
package mainimport ("fmt"
"math"
)type Point struct {
X float64
Y float64
}func (p Point) EuclideanDistanceFromOrigin() float64 {
return math.Sqrt(math.Pow(p.X, 2) + math.Pow(p.Y, 2))
}func main () {
point := Point{4.2, 5.7}
fmt.Println(point.EuclideanDistanceFromOrigin())
point = Point{7.4, 8.1}
fmt.Println(point.EuclideanDistanceFromOrigin())
}
高階函數(shù) (Higher-order functions)
Go embraces higher-order functions, i.e. a function that takes one (or more) function as an argument and/or returns a function. Let’s see an example of both cases:
Go包含高階函數(shù) ,即以一個(gè)(或多個(gè))函數(shù)作為參數(shù)和/或返回一個(gè)函數(shù)的函數(shù)。 讓我們來(lái)看兩種情況的示例:
函數(shù)作為參數(shù) (A function as an argument)
We may pass a function as an argument, such as in the following example:
我們可以將函數(shù)作為參數(shù)傳遞,例如以下示例:
package mainimport "fmt"func sum(a, b int) int {return a + b
}func multiply(a, b int) int {
return a * b
}func printResult(fn func(int, int) int, a, b int) {
val := fn(a, b)
fmt.Println(val)
}func main() {
printResult(sum, 4, 5)
printResult(multiply, 4, 5)
}
Note that we need to pass both the type of function arguments and the type of the function output.
請(qǐng)注意,我們需要同時(shí)傳遞函數(shù)參數(shù)的類(lèi)型和函數(shù)輸出的類(lèi)型。
返回函數(shù) (Returning a function)
Let’s now look at an example of a higher-order function that returns another function:
現(xiàn)在讓我們看一個(gè)返回另一個(gè)函數(shù)的高階函數(shù)的示例:
package mainimport "fmt"func sum(a, b int) int {return a + b
}func multiply(a, b int) int {
return a * b
}func getOperation(name string) func (int, int) int {
if name == "sum" {
return sum
} else if name == "multiply" {
return multiply
}
panic("Operation not supported")
}func main() {
op := getOperation("sum")
fmt.Println(op(4,5))
op = getOperation("multiply")
fmt.Println(op(4,5))
}
Note that, in this case, we are declaring the function type as the return type of the higher-order function.
請(qǐng)注意,在這種情況下,我們將函數(shù)類(lèi)型聲明為高階函數(shù)的返回類(lèi)型。
并發(fā) (Concurrency)
Concurrency is one of the main reasons why many people decide to switch to Go, given its efficiency and simplicity. One of the main concepts to know is the “goroutine”, which is a lightweight thread managed by the Go runtime. A goroutine may be created as simply as follows:
考慮到它的效率和簡(jiǎn)單性,并發(fā)是許多人決定切換到Go的主要原因之一。 要了解的主要概念之一是“ goroutine”,它是Go運(yùn)行時(shí)管理的輕量級(jí)線程。 可以如下簡(jiǎn)單地創(chuàng)建goroutine:
go f(<arg_1>, <arg_2>, ..., <arg_n>)It should be noted that, even though the function f will be executed in the new goroutine, such function (and its arguments) are evaluated in the current goroutine (in fact, the main function runs in a goroutine as well!).
應(yīng)該注意的是,即使函數(shù)f將在新的goroutine中執(zhí)行,該函數(shù)(及其參數(shù))也將在當(dāng)前goroutine中進(jìn)行評(píng)估(實(shí)際上,main函數(shù)也在goroutine中運(yùn)行!)。
In order to allow two or more goroutines to communicate, we’ll have to make use of Go’s channels, which allow us to send and receive information from it. A channel may be created with the following syntax:
為了允許兩個(gè)或更多goroutine進(jìn)行通信,我們必須使用Go的通道,該通道允許我們從中發(fā)送和接收信息。 可以使用以下語(yǔ)法創(chuàng)建頻道:
<variable> := make(chan <type>[, <buffer_size>])Note that you may, optionally, define a buffer size. Send operations will be blocked if the buffer is full.
請(qǐng)注意,您可以選擇定義緩沖區(qū)大小。 如果緩沖區(qū)已滿(mǎn),則發(fā)送操作將被阻止。
Values may be received/sent from/to a channel using the arrow operator <-. To send a value to a channel we would use the following syntax:
可以使用箭頭運(yùn)算符<-從/從通道接收值/向通道發(fā)送值。 要將值發(fā)送到通道,我們將使用以下語(yǔ)法:
<channel> <- <value>Instead, receiving a value from a channel and assigning it to a variable would look as follows:
相反,從通道接收值并將其分配給變量將如下所示:
<variable> := <- <channel>Note that the information always flows in the direction of the arrow.
請(qǐng)注意,信息始終沿箭頭方向流動(dòng)。
The use of channels also helps us in managing synchronization, without using any explicit locks or condition variables, given that send and receive operations block until the other side is ready.
使用通道還可以幫助我們管理同步,而無(wú)需使用任何顯式的鎖或條件變量,因?yàn)榘l(fā)送和接收操作會(huì)阻塞到另一端準(zhǔn)備好為止。
Let’s wrap up everything we’ve seen so far with an example:
讓我們用示例總結(jié)到目前為止所看到的一切:
package mainimport ("fmt"
"math/rand"
)func randint(max int, c chan int) {
r := rand.Intn(max)
fmt.Println(r)
c <- r
}func main() {
c := make(chan int)
go randint(1000, c)
go randint(200, c) r1 := <- c
r2 := <- c
fmt.Println(r1 + r2)
}
In this example we’ve created a function that gets a random number and sends it to our channel. Then, we’ve called such function twice, each time running in its own goroutine. Finally, we’ve received the last two values of the channel, kept them in r1 and r2, and printed out the sum.
在此示例中,我們創(chuàng)建了一個(gè)獲取隨機(jī)數(shù)并將其發(fā)送到我們的頻道的函數(shù)。 然后,我們兩次調(diào)用了此類(lèi)函數(shù),每次都在其自己的goroutine中運(yùn)行。 最后,我們收到了通道的最后兩個(gè)值,將它們保留在r1和r2 ,并打印出總和。
Also note that, for instance, the second printed number may be greater than 200. This would happen because each function call runs in a separate thread (or goroutine), and therefore they may not finish in the same order in which we have called them.
還要注意,例如,第二個(gè)打印的數(shù)字可能大于200。之所以會(huì)發(fā)生這種情況,是因?yàn)槊總€(gè)函數(shù)調(diào)用都在一個(gè)單獨(dú)的線程(或goroutine)中運(yùn)行,因此它們的執(zhí)行順序可能與我們調(diào)用它們的順序不同。
Let’s now see what happens if we attempt to exceed the buffer size:
現(xiàn)在讓我們看看如果嘗試超過(guò)緩沖區(qū)大小會(huì)發(fā)生什么:
package mainimport ("fmt"
)func main() {
c := make(chan int, 2)
c <- 1
c <- 5
c <- 4 r1 := <- c
r2 := <- c
r3 := <- c
fmt.Println(r1 + r2 + r3)
}
In this example we’ve defined a buffer size of 2, but then we’ve attempted to send 3 values to the channel. As expected, this makes our script to crash. Given that the buffer is full, the instruction c <- 4 is blocked until some value is received from the channel. Therefore, we may easily fix our script as follows:
在此示例中,我們將緩沖區(qū)大小定義為2,但隨后嘗試將3個(gè)值發(fā)送到通道。 不出所料,這使我們的腳本崩潰了。 給定緩沖區(qū)已滿(mǎn),指令c <- 4會(huì)被阻塞,直到從通道接收到某個(gè)值為止。 因此,我們可以按以下步驟輕松修復(fù)腳本:
package mainimport ("fmt"
)func main() {
c := make(chan int, 2)
c <- 1
c <- 5
r1 := <- c c <- 4
r2 := <- c
r3 := <- c
fmt.Println(r1 + r2 + r3)
}
As you can see, we’ve just had to receive a value from the channel when the buffer was full, and then we’ve been able to send a new one to it without any issues.
如您所見(jiàn),當(dāng)緩沖區(qū)已滿(mǎn)時(shí),我們只需要從通道接收一個(gè)值,然后就可以向它發(fā)送一個(gè)新值而沒(méi)有任何問(wèn)題。
A channel may be closed (always by the sender) to indicate that no more values will be sent to it. This may be done with the following syntax:
通道可能會(huì)關(guān)閉(發(fā)送者始終會(huì)關(guān)閉)以指示不會(huì)再發(fā)送任何值。 可以使用以下語(yǔ)法完成此操作:
close(<channel>)The receiver may then check whether the channel is closed or not. This may be achieved by defining a second variable in the receiving operation, which will be set to false if there are no more values to receive and the channel is closed. Let’s look at this with an example:
接收器然后可以檢查通道是否關(guān)閉。 這可以通過(guò)在接收操作中定義第二個(gè)變量來(lái)實(shí)現(xiàn),如果沒(méi)有更多要接收的值并且通道已關(guān)閉,則將其設(shè)置為false。 讓我們來(lái)看一個(gè)例子:
package mainimport ("fmt"
"math/rand"
)func randints(max int, c chan int) {
c <- rand.Intn(max)
c <- rand.Intn(max)
c <- rand.Intn(max)
close(c)
}func main() {
c := make(chan int)
go randints(1000, c) r1, ok1 := <- c
fmt.Println(r1, ok1)
r2, ok2 := <- c
fmt.Println(r2, ok2)
r3, ok3 := <- c
fmt.Println(r3, ok3)
r4, ok4 := <- c
fmt.Println(r4, ok4)
}
In this example we’ve sent 3 values to the channel and then read them, always checking whether the channel is closed. In fact, the fourth time we’ve received from the channel, we get that ok4 is equal to false, meaning that the channel is closed. All in all, this means that we could continue reading values from the channel as long as our "ok" variable is true. This is where range comes in, allowing us to iterate over the values of a channel until it is closed. Let's see how we may simplify the above script with an example:
在此示例中,我們向通道發(fā)送了3個(gè)值,然后讀取它們,并始終檢查通道是否關(guān)閉。 實(shí)際上,第四次從通道接收到信號(hào)時(shí),我們確定ok4等于false,這意味著通道已關(guān)閉。 總而言之,這意味著只要我們的“ ok”變量為true,就可以繼續(xù)從通道讀取值。 這就是range所在,允許我們遍歷通道的值,直到通道關(guān)閉為止。 讓我們來(lái)看一個(gè)示例如何簡(jiǎn)化上面的腳本:
package mainimport ("fmt"
"math/rand"
)func randints(max int, c chan int) {
c <- rand.Intn(max)
c <- rand.Intn(max)
c <- rand.Intn(max)
close(c)
}func main() {
c := make(chan int)
go randints(1000, c) for i := range c {
fmt.Println(i)
}
}
In a much cleaner way, we’ve iterated through the channel until it was closed.
以一種更加干凈的方式,我們遍歷了通道,直到通道被關(guān)閉。
怎么辦? (Now, what?)
First of all, congratulations if you’ve made it this far! You may now be wondering, how can I continue learning about Go? Well, I’m one of those who think that getting your hands dirty is one of the best ways to learn. Nothing replaces working towards something, finding issues in your path and solving them yourself.
首先,恭喜您已經(jīng)做到了! 您現(xiàn)在可能想知道,我如何才能繼續(xù)學(xué)習(xí)Go? 好吧,我是那些認(rèn)為弄臟自己的手是最好的學(xué)習(xí)方法之一的人之一。 沒(méi)有什么能代替在某件事上進(jìn)行工作,在您的道路上發(fā)現(xiàn)問(wèn)題并自己解決問(wèn)題。
If you, however, want to continue reading about Go, you may check out these resources:
但是,如果您想繼續(xù)閱讀Go,可以查看以下資源:
Go’s official documentation
Go的官方文檔
Effective Go
有效執(zhí)行
Go by Example
舉個(gè)例子
Google I/O 2012 — Go Concurrency Patterns
Google I / O 2012-Go并發(fā)模式
As long as I discover more interesting learning resources, I will keep adding them to this list.
只要發(fā)現(xiàn)更多有趣的學(xué)習(xí)資源,我就會(huì)一直將它們添加到此列表中。
Now it’s your turn to Go have fun with Go!
現(xiàn)在輪到您去玩吧!
翻譯自: https://towardsdatascience.com/an-introduction-to-golang-7369339dba3
golang簡(jiǎn)介
總結(jié)
以上是生活随笔為你收集整理的golang简介_Golang简介的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 批量修改RTX腾讯通用户密码
- 下一篇: cocos2d 简单消除游戏算法 (一)