The introduction

Source & Introduction

Official link: gobyexample.com/

Gobyexample-cn.github. IO /

As the name implies, introduce/learn the Go language by examples

About this series

This series is divided into the first, middle and second parts, corresponding to:

  • 01~21 Basic Syntax (without goroutines/channel)
  • 22 to 39 Concurrent programming
  • 40 + Common methods (common development scenarios, built-in functions used, etc.)

Github code: github.com/ftopia2020/…

$ git clone [email protected]:ftopia2020/go_by_examples.git Git clone to local➜ Go Go version Go version go1.14.2 Darwin/AMd64 # Personal Golang versionCopy the code

The following articles are my general notes on examples:

  1. Map (includes sample code and comments + screenshots after print run)
  2. Minor changes will be made to Examples + personal reference comments

Not suitable (opinion only) :

  • Development without any programming foundation, direct example learning still requires some programming foundation (mainly theoretical concept understanding)
  • Not recommended as Golang system learning material, system learning recommended Effective Go

Suitable for:

The crowd advice
Other language basics, Golang beginners It is suggested as an extension of systematic learning (e.g. combiningEffective Go)
Golang primary development Each example runs & thinks, without looking at Output, guess & compare
Golang medium and advanced development Can be used as a leak, scan = “can be

01 Hello World

$ go run 01_hello-world.go     Execute the golang file
$ go build 01_hello-world.go   # go build = binary
Copy the code

02 Values

Basic data types: string, integer, floating point, Boolean

Only one main function can be defined, and the example is for ease of operation go run XXX.go

03 Variables

The variable declaration must remember the initial value of the underlying type string – “” integer/floating-point – 0 Boolean – false

Derived types start with nil pointer, channel, func, interface; Array is []; The dictionary for the map []

04 Const

With the “reflect” package, printing constant types is easy to understand: numeric constants have no definite type until they are given

05 For

Later in the tutorial, you’ll see some other forms of for as you learn about range statements, channels, and other data structures.

06 If/else

Go does not provide the ternary operator; it is implemented using if else

07 Switch

The examples here introduce the concepts of functions (func) and interfaces (Interface)

Beginners can learn the 12 Functions and 20 Interfaces before understanding them

08 Array

Array must be fixed length, does not support expansion, in actual development, slice is used more, see ↓

09 Slice

👍 Recommended reading articles by the Go team blog.golang.org/slices-intr… To learn more about the design and implementation details of Slice in Go

10 Map

11 Range

For Golang Strings, read more about Strings, bytes, runes and characters in Go

12 Functions

13 Multiple return values

14 Variadic functions

15 Closures

To understand “closures” and use scenarios, an example is not enough. It is recommended to read related materials for further understanding

By reading the source code for built-in functions or some frameworks, it is easy to see the use of closures, which can be useful in functional programming

The following example also uses closures and supports an important feature called recursion

16 Recursion

As with “closures”, understanding and applying recursion is not a matter of language, but a programming idea and algorithm that must be mastered

17 Pointers

Pointer part, beginners may be a little difficult to understand (C language and other related basic development is not strange ~), it is recommended to read related materials

18 Structs

19 Methods

Go has concepts of types (structs) and methods (methods) for object-oriented programming, but it has no inheritance

The interface implementation of Go is different from that of other programming languages, and the original intention of Go developers is to make it easy to use and versatile

The following is an example of Go’s Interface Interface

20 Interfaces

21 Errors

Golang is simple for error handling, and the example above is just a simple application.

For more information on Golang error handling, see blog.golang.org/error-handl…

Next

The above example covers the basic syntax of Golang (with the exception of Goroutines & Channel for concurrent programming)

After three days of Go by Example (middle), will specifically start Golang concurrent programming examples, but also part of Golang charm ~