The beauty of Go language

Source: Internet
Author: User
Tags try catch cloudflare cockroachdb datadog
This is a creation in Article, where the information may have evolved or changed. Recently, I started exploring the go language when I was doing my hobby, and was conquered by the beauty of Go language. The beauty of the go language is that it has a good balance between flexible use (common to some dynamic, interpreted language) and security (common to some static, compiled languages). In addition, there are two additional features that make me feel that the Go language is ideal for modern software development. I will explain in the section below the advantages. One of them is * * First-class support for language concurrency * * (through ' goroutine ', and ' channels ' implementations, explained below). concurrency, through its design, enables you to efficiently use your CPU horsepower. Even if your processor has only 1 cores, a concurrent design allows you to use the kernel efficiently. That's why you can typically run hundreds of thousands of concurrent ' goroutines ' (lightweight threads) on a single machine. ' Channels ' and ' goroutines ' are at the heart of distributed systems because they abstract the producer-consumer message paradigm. Another feature that I really like about Go is the interface ' (Interface) '. * * Interface provides loosely coupled or decoupled components for your system. * * This means that part of your code can depend only on the interface type, and does not care who implements the interface or how the interface is implemented. Your controller can then provide a dependency on the code that satisfies the interface, which implements all the functionality in the interface. This also provides a very clean architecture for unit testing (through dependency injection). Your controller can now inject a mock implementation of the interface required by the code to be able to test whether it performs its work correctly. I think Go is a great language. Especially for use cases like cloud system development (WEB server, CDN, cache, etc.), distributed systems, microservices, etc. So if you're an engineer or a startup trying to decide the language you want to explore or try, take a serious look at Go. In this article, I'll discuss the following aspects of the language: a) Introduction B) Why do I need go c) target audience D) Go Advantage e) Go's weakness F) go to 2 g) goes design concept h) How to start I) who is using go## introduction Go is an open source language by Robert Griese Mer,rob Pike and Ken Thompson were created at Google. Open source code means that everyone can contribute to the language by providing [recommendations] (Https://github.com/golang/go/wiki/ExperienceReports) for new features, fixing errors, and so on. The language code is on [GitHub] (https://GITHUB.COM/GOLANG/GO). Here (https://golang.org/doc/contribute.html) provides documentation on how to contribute to the language. # # Why do I need to go the author mentions that the main motive for designing a new language is to solve Google's internal software engineering problems. They also mentioned that Go was actually developed as a substitute for C + +. Rob Pike mentions the purpose of the Go programming language:> "so the purpose of GO is not to study programming language design; It is designed to improve the working environment of designers and co-workers. Go is more concerned with software engineering than the study of programming languages. Or, in other words, a language designed to serve software engineers. * * * (excerpt from (https://talks.golang.org/2012/splash.article)): a) Slow build-build sometimes takes one hours to complete b) uncontrolled dependencies c) Each programmer uses a different subset of the language (d) program to understand poorly (code difficult to read, improper recording, etc.) e) Repeat effort f) updated cost g) version skew h) Write automatic tool difficulty i) cross-language build * * To be successful, go must address these issues * * (excerpt from (https:// talks.golang.org/2012/splash.article)): A) Go must be able to be used on a large scale, for large groups of people, and for projects with a large number of dependent programs. b) Go syntax must be familiar, roughly class C. Google needs to quickly improve the efficiency of programmers in go, which means that language grammar changes cannot be too radical. c) go must be modern. It should have features like concurrency so that programs can use multicore machines efficiently. It should have a built-in network and Web server library so that it helps modernize the development. # # Target Listener Go is a system programming language. For things like cloud systems (network servers, caches), microservices, distributed systems (due to concurrency support), Go is really good. # # Advantage **a) Static type: * * Go is static type. This means that you need to declare the type at compile time for all variables and function parameters (and return variables). While this may not sound convenient, it has a big advantage, because it will find many errors at compile time. This factor plays a very important role when your team size increases, because the type of declaration makes the function and library easier to read and easier to understand. **B) Compile Speed: **go code Compilation Speed * * Very fast * *, so you do not have to wait for code compilation. In fact, the ' Go Run ' command will start your go program very quickly, so youDon't even think your code is compiled first. It feels like an explanatory language. **C) Execution Speed: * * Depending on the operating system (LINUX/WINDOWS/MAC) and the CPU instruction set architecture of the machine the Code is compiling (x86,x86-64,arm, etc.), the Go code is compiled directly into machine code. So, it runs very fast. **D) Portable: * * Because the code is compiled directly into machine code, binary files become portable. Portability here means that you can get binaries from your machine (like linux,x86-64) and run it directly on your server (if your server is running Linux on the x86-64 architecture). Because the Go binaries are statically linked, this means that any shared operating system libraries that your program requires will be included in the binaries at compile time. They are not dynamically linked when they run the program. This is a huge benefit for deploying programs on multiple machines in the data center. If you have 100 machines in your datacenter, simply compile your binaries into the same operating system and instruction set architecture that your machine runs, and you can easily "SCP" your program binaries to all of these machines. You don't need to worry about the Linux version they're running. There is no need to check/manage dependencies. During the run of the binaries, all the programs are running. **E) Concurrency: * * Go has first-class support for concurrency. Concurrency is one of the main selling points of Go. The language designer has designed the concurrency model around Tonihol's ["Communicating Sequential Processes"] (http://www.cs.cmu.edu/~crary/819-f09/Hoare78.pdf) thesis. The **go runtime allows you to run hundreds of thousands of concurrent ' goroutine ' on a machine. * * ' goroutine ' is a lightweight thread of execution. The Go runtime re-uses these ' goroutine ' on the operating system thread. This means that multiple ' goroutine ' can run concurrently on a single operating system thread. The Go runtime has a scheduler whose task is to dispatch these ' goroutine ' executions. There are two benefits to this approach: 1. The ' Goroutine ' at initialization has a 2KB stack. This is very small compared to a typical 1 MB OS thread stack. This number is important when you need to run hundreds of thousands of different goroutine at the same time. If you are running thousands of OS threads in parallel, RAM will obviously become a bottleneck. 2. Go can follow the same model as Java and other languages, it supports the OS lineThreads with the same thread concept. In this case, however, the context switching cost between OS threads is much larger than the context switching cost between different ' goroutine '. Because I mentioned "concurrency" several times in this article, I recommend that you review Rob Pike's discussion about [concurrency is not parallelism] (HTTPS://WWW.YOUTUBE.COM/WATCH?V=CN_DPYBZKSO). In programming, concurrency is the composition of processes that are executed independently, while parallelism is (possibly related to) the simultaneous execution of computations. Unless you have a processor with multiple cores or have multiple processors, you can't really have parallelism because the CPU core can only do one thing at a time. On a single core machine, concurrency is the only way to work behind the scenes. The OS scheduler schedules different processes for different time slices on the processor (actually threads, at least one main thread per process). Therefore, at some point, you can only run one thread (process) on the processor. As the instructions were executed quickly, we sensed that there were a lot of things running. But actually it's just one thing. Concurrency is the process of dealing with many things at once. Parallelism is doing a lot of things at once. **F) Interface: * * interface makes it possible to loosely-coupled systems. The interface type in Go can be defined as a set of functions. Only. Any type that implements these functions implicitly implements the interface, that is, does not require the specified type to implement the interface. This is checked automatically by the compiler at compile time. This means that part of your code can depend on only one interface type, and does not care about who implements the interface or how the interface is implemented. Your master/Controller function can then provide a dependency that satisfies the interface (implementing all functions in the interface). This also provides a very clean architecture for unit testing (through dependency injection). Now your test code can inject a mock implementation of the interface required by the code to be able to test whether it performs its work correctly. While this is great for decoupling, another benefit is that you can start to see your architecture as a different microservices. Even if your application resides on a single server (if you're just starting), you can design the different features you need in your application as different microservices, each of which implements the interface it promises. So other services/controllers just call the methods in the interface, rather than actually care how they are implemented behind the scenes. **G) Garbage Collection: * * Unlike C, you don't need to remember to release the pointer or worry about hanging the pointer in Go. The garbage collector does the work automatically. **H) do not error exception, self-processing errors: * * I like Go no other language has standard exception logic. Instead of having them wrap all the code in a try catch block, forcing developers to handle basic errors such as "Unable to open files". This also forces developers to consider what needs to be done to address these failure conditions. **i)Amazing tool * *: one of the best things about go is its tools. It has the following tools: i) [gofmt] (Https://blog.golang.org/go-fmt-your-code): It automatically formats and indents your code so that your code looks like every go developer on the planet. This has a huge impact on the readability of the code. II) [Go Run] (https://golang.org/cmd/go/#hdr-compile_and_run_go_program): Compile your code and run them, all:). So, even if Go needs to be compiled, the tool makes you think it's an interpreted language because it compiles your code so quickly that you don't even feel it when the current code is compiled. III) [Go get] (https://golang.org/cmd/go/#hdr-download_and_install_packages_and_dependencies): Download the library from GitHub and copy it to Gopath, so that you can import the library into the Project IV) [Godoc] (Https://godoc.org/golang.org/x/tools/cmd/godoc): Godoc Parse your Go source code-including comments-and in HTML or plain text format to generate its documents. With the Godoc Web interface, you can see documents that are tightly integrated with the code you record. With a single click, you can navigate from the document of the function to its implementation. You can see more [tools] (https://golang.org/cmd/go/) here. **K) excellent built-in library: * * Go has a great internal library to help with current software development. Some of them are: a) [Net/http] (https://golang.org/pkg/net/http/)-Provides HTTP client and server implementation B) [Database/sql] (https://golang.org/pkg /database/sql/)-for interacting with SQL database C) Encoding/json-json is considered the first class member of the standard language:) d) html/templates-html Template Library E) Io/ioutil-Implement I/O real There is a lot of progress in the development of the program function go. You can find all the go libraries and frames in [here] (https://github.com/avelino/awesome-go) for eachTools and use cases. # # # Weakness **1. generics lack of * *-generics let us design the algorithm at a later time specifying the type to be specified. Suppose you need to write a function to sort the list of integers. Later, you need to write another function to sort the list of strings. At that moment, you realize that the code almost looks the same, but you can't use the original function, because the function can take an integer type list or a type string list as a parameter. This will require code duplication. Therefore, generics allow you to design algorithms around types that you can specify later. You can design an algorithm to sort the list of type T. You can then use integer/string/any other type to call the same function because there is a sort function for that type. This means that the compiler can check if one value of that type is greater than another value of that type (because this is required for sorting) by using the language's null interface ' (interface {}) ' feature, you can implement some common mechanism in Go. However, this is not ideal. Generics are a controversial topic. Some programmers swear. Others do not want them to be included in the language, because generics are often a tradeoff between compilation time and execution time. That is to say, the author of Go in Go expresses the openness to implement some kind of generic mechanism. However, this is not just generics. Generics can only be implemented if all other features of the language are working correctly. Let's wait and see if Go 2 offers them some sort of solution. **2. Lack of dependency Management * *-GO1 's commitment means that the go language and its libraries cannot change their APIs during the life cycle of go 1. This means that your source code will continue to compile for Go 1.5 and go 1.9. As a result, most third-party Go libraries follow the same promise. Since the primary way to get third-party libraries from GitHub is through the ' Go get ' tool, when you execute ' go get github.com/vendor/library ', you want the latest code in their main branch to not change the library API. While this is cool for temporary projects, most libraries do not violate commitments, but this is not ideal for production deployments. Ideally there should be some version dependent methods, so you can simply include the version number of the third-party library in your dependent file. Even if their API changes, you don't need to worry because the new API will have a newer version. You can later look back at your changes and decide whether to upgrade the versions in your dependent files and change your client code based on the changes in the API interface. Go's official experiment [DEP] (HTTPS://GITHUB.COM/GOLANG/DEP) should quickly become a solution to this problem. Probably in Go 2. (VGo will solve this problem) # # go 2 Development I am notThe authors often like to use this open source approach to implement this language. If you want to implement a feature in Go 2, you need to write a document that you need to: a) Describe your use case or question B) to describe how the go cannot solve a use case/problem C) Describes how big a problem is (some problems are not big enough or there is not enough focus to solve a particular time problem). D) If possible, the author will review and link it to [here] (Https://github.com/golang/go/wiki/ExperienceReports) to propose solutions to the problem. All discussions on the issue will be conducted on public media such as mailing lists and issue trackers. In my opinion, the two most pressing problems of language are generics and dependency management. Dependency management is more about publishing engineering or tooling issues. Hopefully we'll see [DEP] (HTTPS://GITHUB.COM/GOLANG/DEP) (official experiment) as the official tool to solve the problem. Since the author has expressed openness to the generics of the language, I wonder how they are implemented because generics are at the expense of compile time or execution time. # # Go design concept in Rob Pike's talk ["Simple is Complex"] (https://talks.golang.org/2015/simplicity-is-complicated.slide#18), Some of the design ideas he mentioned have made me feel very bright. Specifically, I like the following: a) * * Traditionally, other languages want to add new features constantly. * * In this way, all languages simply add to the bloat, and the complexity of the compiler and its specifications is too high. If this continues, each language will look the same in the future, as each language will constantly add features it does not have. Consider that JavaScript adds object-oriented functionality. The Go author intentionally does not include many features in the language. * * Only the characteristics of those who agree on the author are included, for those who truly feel that they are actually bringing value to what the language can achieve. **B) * * Features orthogonal vectors in the solution space. * * It is important to be able to select and combine different vectors for your use case. * * and these vectors should naturally interact with each other. means that each feature of the language can work with any other predictable feature. * * In this way, these feature sets cover the entire solution space. Implementing all of these very natural features brings a lot of complexity to language implementations. But the language abstracts complexity and provides you with an easy-to-understand interface. Therefore, simplicity is the art of hiding complexity c) * * The importance of readability is often underestimated. * * Readability is one of the most important things in designing a programming language because maintaining the importance of software andThis is very high. Excessive functionality can affect the readability of the language. d) * * readability also means reliability. * * If a language is complicated, you have to learn more to read and write code. Again, you have to debug it and be able to fix it. This also means that new developers on your team will need to expand their time to understand the language until they can contribute to your codebase. # # How to get started you can download go and follow the installation instructions here. Here is the [official Guide] to get started with Go (HTTPS://TOUR.GOLANG.ORG/WELCOME/1). [Go by example] (https://gobyexample.com/) is also a good book. If you want to read a book, [The Go programming Language] (https://www.amazon.com/ programming-language-addison-wesley-professional-computing/dp/013419044) is a good choice. It is written in the same way as the legendary [C language white Paper] (https://www.amazon.com/Programming-Language-2nd-Brian-Kernighan/dp/0131103628), by [Alan A. A . Donovan] (https://www.informit.com/authors/bio/cd7c1e12-138d-4bf9-b609-e12e5a7fa866) and [Brian W. Kernighan] (https:/ /en.wikipedia.org/wiki/brian_kernighan) writing. You can join the [Gophers Slack] (https://invite.slack.golangbridge.org/) channel to work with the community and participate in discussions about that language. # # Which companies are using go[many companies] (Https://github.com/golang/go/wiki/GoUsers) has started to invest a lot of money in go using go. Some famous manufacturers include: Google-[Kubernetes] (http://kubernetes.io/), [MySQL Extended Infrastructure] (http://vitess.io/), [dl.google.com] (https:// talks.golang.org/2013/oscon-dl.slide#1) (Download serviceBaseCamp-[Go at BaseCamp] (https://signalvnoise.com/posts/3897-go-at-basecamp) CloudFlare-[blog] (https:// blog.cloudflare.com/go-at-cloudflare/), [ArsTechnica article] (https://arstechnica.com/information-technology/2013/02/ cloudflare-blows-hole-in-laws-of-web-physics-with-go-and-railgun/) Cockroachdb-[Why Go is the right choice for CockroachDB] (https:/ /www.cockroachlabs.com/blog/why-go-was-the-right-choice-for-cockroachdb/) CoreOS-[GitHub] (https://github.com/ coreos/), [blog] (https://blog.gopheracademy.com/birthday-bash-2014/go-at-coreos/) DataDog-[Go at DataDog] (https:// blog.gopheracademy.com/birthday-bash-2014/go-at-datadog/) Digitalocean-[Let your development team start using go] (https:// blog.digitalocean.com/get-your-development-team-started-with-go/) Docker-[Why we decided to write Docker in go] (https:// www.slideshare.net/jpetazzo/docker-and-go-why-did-we-decide-to-write-docker-in-go/) Dropbox-[open source our go library] (https:// blogs.dropbox.com/tech/2014/07/open-sourcing-our-go-libraries/) Parse-[How do we transfer our API from Ruby to go and save Our Sanity] (http:// Blog.parse.com/learn/how-we-moved-our-api-from-ruby-to-go-and-saved-our-sanity/) Facebook-[GitHub] (https://github.com/ facebookgo/) Intel-[GitHub] (https://github.com/clearcontainers) Iron.io-[Go after 2 years in production/] (https:// www.iron.io/go-after-2-years-in-production/) MalwareBytes-[processing 1 million requests per minute, using Golang] (http://marcio.io/2015/07/ handling-1-million-requests-per-minute-with-golang/) Medium-[How Medium goes social] (https://medium.engineering/ how-medium-goes-social-b7dbefa6d413) MongoDB-[Go Agent] (https://www.mongodb.com/blog/post/go-agent-go) Mozilla-[ GitHub] (https://github.com/search?o=desc&q=org%3Amozilla+org%3Amozilla-services+org%3Amozilla-it+language% 3ago&ref=searchresults&s=stars&type=repositories&utf8=%e2%9c%93) Netflix-[GitHub] (https:// Github.com/netflix/rend) Pinterest-[GitHub] (Https://github.com/pinterest?language=go) Segment-[GitHub] (https:// Github.com/segmentio?language=go) SendGrid-[How to persuade your company to use with Golang] (Https://sendgrid.com/blog/convince-company-go-golang/) Shopify-[Twitter] (https://twitter.com/burkelibbey/status/312328030670450688) SoundCloud-[ Enter SoundCloud] (https://developers.soundcloud.com/blog/go-at-soundcloud) sourcegraph-[YouTube] (https:// WWW.YOUTUBE.COM/WATCH?V=-DPKAOPZ8L8) Twitter-[handling 500 million meetings per day] (https://blog.twitter.com/engineering/en_us/a/2015/ handling-five-billion-sessions-a-day-in-real-time.html) Uber-[blog] (https://eng.uber.com/go-geofence/), [GitHub] ( Https://github.com/uber?language=go)

via:https://hackernoon.com/the-beauty-of-go-98057e3f0a7d

Author: Kanishk Dudeja Translator: Wentingrohwer proofreading: polaris1119

This article by GCTT original compilation, go language Chinese network honor launches

This article was originally translated by GCTT and the Go Language Chinese network. Also want to join the ranks of translators, for open source to do some of their own contribution? Welcome to join Gctt!
Translation work and translations are published only for the purpose of learning and communication, translation work in accordance with the provisions of the CC-BY-NC-SA agreement, if our work has violated your interests, please contact us promptly.
Welcome to the CC-BY-NC-SA agreement, please mark and keep the original/translation link and author/translator information in the text.
The article only represents the author's knowledge and views, if there are different points of view, please line up downstairs to spit groove

855 Reads
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.