In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what are the basic knowledge of Google Go language". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what are the basic knowledge of Google Go language".
Let's start with hello world.
Hello.go
Package main / / declare the package name of this file import "fmt" / / fmt library of import language-- used to output func main () {var str string = "hello world" / / str: = "hello world" / / var str = "hello word" fmt.Println (str)}
Running
There are two ways to explain the operation.
1. Compile first: go build hello.go
Run again:. / hello
2. You can also compile and run directly (in fact, the following command is actually compiled into hello.out and then executed): go run hello.go
For students who are used to C-series languages, they will be very unaccustomed to the grammar of go. *, go does not use ";" as the statement closing flag; second, go is the variable in front of the type, and variable initialization can also be like the two lines of comments, without specifying the type, the go compiler can derive from the right value of the initialization expression which type the variable should be declared to, which makes go look a bit like a dynamic language, which may also be why some people say it is the reason for python 4.0.
Go is likely to be a language that forcibly unifies the style of the code. For example, the go language requires that the variable name of public must start with an uppercase letter, and the private variable must be switched with a lowercase letter, which not only eliminates the public,private keyword, but more importantly, unifies the style. Also, for judgment statements, if you write something like this:
If str = "descur" {.... } else {.... }
It cannot be compiled and passed, so it must be written like this:
If str = "descusr" {...} else {...}
This may be painful for kids who grow up in the arms of Microsoft, but it's good for code cleanliness addicts like me. In fact, the unity of the code style, teamwork is very useful.
Programming philosophy
C language is purely procedural, which is related to its historical background. The C#/JAVA language is a highly object-oriented language, which typically shows that there are no isolated methods in their system, and these methods must belong to a class. Instead of denying either side, go combines all kinds of programming ideas with critical absorption, integrates the strengths of many families, strives to maintain the simplicity of language features, and strives to be small and refined. The deeper you go into go, you will find that go is really too concise.
From the perspective of programming paradigm, go is a reformer, not a reformist.
Although go is an object-oriented language, there is no object-oriented concept in the concept of go, only structures. Go's classes are highly granular, as in the following code:
Type rect struct {width, height int} func (r * rect) area () int {/ / find area return r.width * r.height} func (r * rect) perimeter () int {/ / find perimeter return 2 * (r.width + r.height)} func main () {r: = rect {width: 10, height: 15} fmt.Println ("area:" R.area () fmt.Println ("perimeter:", r.perimeter ()) rp: = & r fmt.Println ("area:", rp.area ()) fmt.Println ("perimeter:", rp.perimeter ())}
Classes and class methods are completely separate and are called only after the object is initialized, reducing the degree of coupling. Go has no constructors and destructors. Since there are no virtual functions in the go language, there is no vptr, so supporting constructors and destructors is of little value.
Second, the go language opposes the overloading of functions and operators, while JAVA and Centrum allow functions or operators of the same name, as long as their argument lists are different. Although overloading solves a small part of the OOP problem, it places a great burden on these languages, and this approach does not bring much value to solving the problem, so go does not provide overloading.
Thirdly, go opposes inheritance and virtual function overloading. In fact, go also provides inheritance, but uses a combined approach to provide:
Type Car struct {Base...} func (color * Car) Drive () {...}
After giving up a lot of OOP features, go provides a pretty great feature: interfaces. You may wonder, all object-oriented languages also have interfaces? But the interfaces of those object-oriented languages are basically the same, while those of go are different.
One difference between interfaces in go and other languages is that they are non-intrusive. In object-oriented languages such as C #, in order to implement an interface, you need to inherit from the interface, such as:
Public interface IBankAccount {void PayIn (decimal amount);} class SaverAccount: IBankAccount {public void PayIn (decimal amount) {Console.WriteLine ("This is PayIn");}}
In the go language, there is no need to derive from an interface when implementing a class, such as:
Type SaverAccount struct {/ / go...} var saveAccount IBankAccount = new (SaveAccount)
As long as all the methods required by IBankAccount are implemented, the interface is implemented and can be assigned, which is quite atomic.
Thank you for your reading, the above is the content of "what are the basic knowledge of Google Go language". After the study of this article, I believe you have a deeper understanding of what the basic knowledge of Google Go language is, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.