Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the advantages of using Go language

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Most people do not understand the knowledge points of this article "what are the advantages of the use of Go language?", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this article "what are the advantages of the use of Go language?"

Map collections / maps use 0 values by default

When using mapping in Go, you can query a specific key even if you haven't inserted any value for it, and it corresponds to a value of 0 that is not nil, unless it is stored as a pointer.

So if you have such a mapping mvv = map [string] int and you want to get m ["hello"], the result will be 0, even if there is no value at that position.

This allows us to write code without having to check the existence of the key, thus making the code cleaner. Imagine if we want to save the frequency of a character in a string, we just need to do this:

Func count (input string) (map [string] int) {m: = map [string] int {} for _, s: = range input {m [string (s)] + / / this step will always be safe} return m}

In the above code, we do not need to determine whether the key already has a value before incrementing.

Invariant value

By default, when you pass a value to a method or function, you cannot change it. When you do need to change the value, you need to pass a pointer to the value. It is not as strict as Rust, which needs to mark it as mutable when initializing a variable. However, this still means that if you call a function that does not receive pointers, you can be sure that it will not be confused with your structure.

The value will not be nil by default

I don't like Null very much in either language, so I'm glad that in Go, your structure or primitive type will never point to Nil by default, which eliminates error checking-- with one exception, of course, in cases where pointers are used, so we should have an error-handling mechanism in this case. Even Tony Hoare, who invented Null, once described it as a "multimillion-dollar mistake".

Type myStruct struct {} func magic (m myStruct) {/ / has no pointer, in this case Nil} func magicp (m * myStruct) {/ / may have a null pointer! }

Nil slice = = empty slice

Yes, this is related to Nil again, because I really don't like Nil values. So another reason that makes me fall in love with Go is that if you have a nil slice, it's no different from an empty slice, so you don't have to separately determine whether a slice is nil and then determine whether it is empty. This happens to be one of the reasons why I like Common Lisp, and I was surprised that the Go language could also have this feature.

Func main () {var s [] intif len (s) = = 0 {fmt.Println ("hello")}}

Easily publish related libraries

I never seemed to realize the importance of this until I knew how easy it was to publish libraries in the Go language. I have written some Go libraries myself, and the only thing I need to do is push them to GitHub, after which others can reference the project gogetgithub.com/4byte/ {lib} directly in the project. Now you can add them as go modules, but this is as simple as publishing libraries to GitHub.

Go has a strong grammar preference.

People hate the idea of forcing code formats, but Go is a language with a strong syntax preference. Personally, I like its style because it removes a lot of useless parts from the code review. To borrow Rob Pike's exact words:

Gofmt won't be someone's favorite, but it's also everyone's favorite.

Multi-paradigm programming

Much of the Go code you see is object-oriented, but it doesn't have to be object-oriented. In fact, although Go lacks some syntax and functionality in some places, Go can actually be a good functional programming language. For example, the Go language can satisfy Function Currying or continue CPS-style programming.

Although you don't have to make the entire code base functional programming style, it can be used in some parts of the project.

Go community

Whether on freenode/go-nuts on IRC or on gophers.slack.com, you can find a lot of developers who really care about the language. And these communities are friendly to newcomers who have just moved to Go. What is even more impressive is that the Go community has made great efforts in terms of diversity and inclusiveness, such as the GoBridge project and the "Women Who Go" project on Git.

Run everywhere

Go can run almost anywhere, and you can build it on any platform as long as you provide the correct GOOS,GOARCH,CGO_ENABLED or GOARM tags, etc. This allows Go to run on a variety of architectures, such as amd64386 and arm.

Apart from this, Go is also the first language to have built-in WebAssembly. In addition to these platforms that are already supported, there are projects like TinyGo that are promoting Go's more powerful cross-platform and portability.

Go is specially designed as a simple language

Go has been aiming for simplicity since its inception, and its simplicity will be very successful in its current position. The language specification of Go is something you can fully understand from beginning to end (it doesn't take much time), which means that some features that can be found in other languages do not exist in Go, one of the more controversial is the lack of generics in Go.

The above is about the content of this article on "what are the advantages of using Go language?" I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report