Directory:

  • 01. Why do you choose Go
  • 【 hand by hand teach you to write 】02
  • 03. Basic data types
  • 04. Operators and flow control
  • 05. Function
  • 06. Project management
  • 07. Compound types – Pointers
  • 08. Compound types – arrays and slices
  • 08. Complex types -map
  • 09. Complex types – Structures
  • 10. Object-oriented programming
  • 11. Exception handling
  • 12. Text processing
  • 13. Concurrent programming – concurrent entry

The rise of a language must have his reasons, the so-called world suffering Java for a long time, the arrival of Go can be said to be a lot of back-end development Gospel, especially in micro services, distributed so hot today, then, what is his advantage?

First, we need the pain points of an existing backend language:

In fact, the advantages and pain points of each language are often the same. After all, there is no perfect thing in this world. If you want to enjoy it well, you have to bear its pain.

  • PHP, scripting language, development speed is fast, and high reusable, if it is to write full stack code, can be said to be a magic tool.
    • Scripting languages that are fast to write and slow to run. The advantage is that upload real-time effect, do not need to restart. The downside is run-time compilation, which is inefficient. (If you’re starting a small business, or if you’re just building a small website, PHP is really the best choice.)
    • One request per process. Don’t worry about concurrency, which is the biggest problem with PHPer going. This also naturally limits the amount of concurrency in PHP.
    • Swoole has greatly improved THE efficiency of PHP, but concurrency, asynchrony, timers and the like are still not enough.
  • Java, strongly typed, compiled language.
    • Java can be said to be an industrial language, is a knife, if the business volume of enterprises up, using Java refactoring is a good choice. But again, this makes it run from a very high starting-point, maybe my site requests are using 2% of the CPU, and the JVM is using 20%.
    • Development is slow. Strong typing, long compilation time, and starting from class make development relatively slow.
  • Go, small but strong.
    • In my opinion, Go is the C of the server world. It’s small, but it’s strong. See my article on C. 【 rewatch C language 】 Hello world
    • Fast compilation, one key asynchronous, own timer, channel, coroutine, lock. Let’s say the hair that was lost because of interprocess relationships grew back.
    • The package is small, dependency free, and can be run directly on the server, which makes phper’s php.ini and Nginx configurations a lot easier.

Go has a promising future. However, I hope that when you learn a language, do not just because a lot of people learn, to understand that language is just a tool. For example, if I were to write a background now, I would still use PHP. Why? Query easy to write, fast development, but also directly full stack. Finding the strengths and essence of a language is the point.

Many people learn a language by looking at the syntax, looking at the framework, seeing how CURD is written, translating the original query, and done.

Okay, let’s get down to business and start learning the Go language.

1. Learn Go for the first time

1.1 Introduction to Go

1.1.1 What is the Go language

On November 10, 2009, Go officially became a member of the open source programming language family.

Go, or Golang, is the C language of the cloud computing era. Go language was born to allow programmers to have higher productivity, Go language specifically for multi-processor system application programming is optimized, the use of Go compiled programs can be as fast as C or C++ code, but also more secure, support parallel process.

When choosing a language for a project, developers have to choose between fast development and performance. Languages like C and C++ offer fast execution speeds, while languages like Ruby and Python excel at rapid development. The Go language Bridges the gap, providing a high-performance language while also making development faster.

Go is a compiled language with concurrent support and garbage collection.

In a sense, it’s an optimized version of C.

1.1.2 Go Language advantages

  • Directly compiled to machine code, independent of other libraries, the version of Glibc requires that deployment be done by throwing a file on it.

  • A statically typed language has the feel of a dynamic language, where most of the hidden problems can be checked out at compile time, and a dynamic language has the feel of a large number of packages that can be used and written efficiently.

  • Concurrency is supported at the language level, and this is the biggest feature of Go, which naturally supports concurrency. Go is the concurrency supported by genes that can take full advantage of multiple cores, making it easy to use concurrency.

  • The built-in Runtime supports garbage collection, which is one of the features of dynamic languages. While GC is not perfect at the moment, it is sufficient for most situations we can encounter, especially GC after Go1.1.

  • The Go keyword is 25, but very expressive and supports almost all the features you’ve seen in other languages: inheritance, overloading, objects, etc.

  • Rich standard library, Go has built a large number of libraries, especially the network library is very powerful.

  • Built-in powerful tools, Go language built-in many tool chains, the best should be gofmt tool, automatic formatting code, can make the team review so easy, the code format is the same, it is difficult to think different.

  • Cross-platform compilation: If you write Go code that does not include CGO, then Windows can compile Linux applications. How do you do this? Go references the code for plan9, which is system independent information.

  • Built-in C support, Go can also include C code directly, using existing rich C libraries.

1.1.3 What is Go suitable for

  • Server programming, Go is a good place to do things you used to do in C or C++, such as logging, data packaging, virtual machine processing, file systems, etc.

  • Distributed systems, database agents, etc.

  • Network programming, this piece of the most widely used, including Web applications, API applications, download applications.

  • In-memory databases, such as Google developed Groupcache, are part of Couchbase.

  • Cloud platform, at present, many foreign cloud platforms are using Go development, CloudFoundy part of the construction, the former VMare technical director out of his own apcera cloud platform.

1.1.4 Language features of Go

  1. Retain but greatly simplify Pointers
  2. Multiparameter return
  3. Built-in basic data structures such as array Slice Map
  4. Panic Recover Error
  5. interface
  6. goroutine
  7. Multi-core processing and network development
  8. Partial functional programming (anonymous functions, closures), reflection, language interactivity
  9. Package the directory

 

1.1.5 Philosophy of Go

Wiring: How are components coupled and how are components wired together

Composition: The basis for forming composite objects.

Less can be more

Avenue to simple, small and yun zhen

It’s easy to make things complicated, hard to make things simple, deep engineering culture.

conclusion

If something is wrong, please point it out.

If you don’t understand, please point out and LET me add chestnuts.

If you feel OK, you can like it and let more people see it.

Recommended reading

  • The first big thing to learn about MySQL is indexes
  • Learn the second mountain of MySQL – lock, transaction
  • Learn the last big hurdle in mysql – table design
  • Why is MySQL using B+ tree?