The difference between go and other languages, what is the difference between go and Java, Python

Zero, go and other languages

0. What is object-oriented

Before knowing whether the Go language is object-oriented (abbreviation: OOP), we must first know what OOP is, and we must first "define" it

According to Wikipedia's definition, we sort out several basic cognitions of OOP:

  • Object-Oriented Programming (OOP) is a programming paradigm based on the concept of an "object," which can contain both data and code: data in the form of fields (often called attributes or attributes), and code in the form of programs (often called method).
  • An object's own program can access and often modify its own data fields.
  • An object is often defined as an instance of a class.
  • Objects take advantage of private/protected/public visibility of properties and methods, and the internal state of the object is protected from outside influence (encapsulated).

Based on these basic cognitions, the three basic characteristics of object-oriented are further extended:

  • encapsulation
  • inherit
  • polymorphism

1. What is the difference between Go language and Java?

1. Function overloading is not allowed on Go, and must have unique names for methods and functions, while Java allows function overloading.

2. In terms of speed, Go is faster than Java.

3. Java allows polymorphism by default, but Go does not.

4. The Go language uses the HTTP protocol for routing configuration, while Java uses Akka.routing.ConsistentHashingRouter and Akka.routing.ScatterGatherFirstCompletedRouter for routing configuration.

5. Go code can automatically scale to multiple cores, while Java is not always scalable enough.

6. The inheritance of Go language is completed through anonymous combination. The base class is defined in the form of Struct. The subclass only needs to put the base class as a member in the definition of the subclass to support multiple inheritance; while the inheritance of Java is completed through the extends keyword. Multiple inheritance is not supported.

2. Is Go an object-oriented language?

Yes and no. Because:

  1. Go has types and methods, and allows an object-oriented programming style, but no type hierarchy.
  2. The "interface" concept in Go offers a different approach that we think is easy to use and in some ways more general. There are also ways to embed types within other types to provide something similar, but not equivalent to subclassing.
  3. Methods in Go are more general than those in C++ or Java: they can be defined for any type of data, even built-in types such as ordinary, "unboxed" integers. They are not limited to structures (classes).
  4. Go Due to the lack of a type hierarchy, "objects" in Go are more lightweight than languages ​​like C++ or Java.

3. Go implements object-oriented programming

encapsulation

"Encapsulation" in object-oriented refers to the ability to hide the internal properties and implementation details of an object, and only provide public interface calls to the outside world, so that users do not need to pay attention to how you implement it internally.

Attribute access rights in the Go language are controlled by initial letter case:

  • The first letter is capitalized, which means it is public and can be accessed externally.
  • The first letter is lowercase, which means it is private and cannot be accessed externally.

An example in the Go language is as follows:

type Animal struct {
	name string
}

func NewAnimal() *Animal {
 	return &Animal{}
}

func (p *Animal) SetName(name string) {
 	p.name = name
}

func (p *Animal) GetName() string {
 	return p.name
}

In the above example, we declare a structure Animal whose attribute name is lowercase. There are Setter and Getter methods on the package, which are used for unified access and setting control.

In this way, the basic encapsulation in the Go language is realized.

inherit

"Inheritance" in object-oriented refers to the subclass inherits the characteristics and behaviors of the parent class, so that the subclass object (instance) has the instance field and method of the parent class, or the subclass inherits the method from the parent class, so that the subclass has the parent class class with the same behavior.

From a practical example, animals are a large parent category, which can be subdivided into "herbivores" and "carnivores", both of which contain the basic definition of the parent category "animal".

From a practical example, animals are a large parent category, which can be subdivided into "herbivores" and "carnivores", both of which contain the basic definition of the parent category "animal".

In the Go language, there is no inheritance method similar to the extends keyword, and the language design adopts a combination method :

type Animal struct {
 	Name string
}

type Cat struct {
 	Animal
 	FeatureA string
}

type Dog struct {
 	Animal
 	FeatureB string
}

In the above example, we declared the Cat and Dog structs, which internally composite the Animal struct anonymously. So both Cat and Dog instances can call methods of the Animal struct:

func main() { 
 	p := NewAnimal() 
 	p.SetName("I am a porter, go to like fried fish~") 

 	dog := Dog{Animal: *p} 
 	fmt.Println(dog.GetName()) 
}

Also instances of Cat and Dog can have their own methods:

func (dog *Dog) HelloWorld() { 
 	fmt.Println("The brain is fried fish") 
} 

func (cat *Cat) HelloWorld() { 
 	fmt.Println("The brain is fried fish") 
}

The above example can normally include the related properties and methods of calling Animal, and can also have its own independent properties and methods, achieving an effect similar to inheritance in the Go language.

polymorphism

polymorphism

"Polymorphism" in object-oriented refers to the ability of the same behavior to have multiple different manifestations or forms. Specifically, it means that the same method of a class instance (object) has different manifestations in different situations.

Polymorphism also allows objects with different internal structures to share the same external interface, that is, a set of external templates, and what is actually inside, as long as it meets the specifications.

In the Go language, polymorphism is implemented through interfaces:

type AnimalSounder interface { 
 	MakeDNA() 
} 

func MakeSomeDNA(animalSounder AnimalSounder) { // The parameter is the AnimalSounder interface type 
 	animalSounder.MakeDNA() 
}

In the above example, we declare an interface type AnimalSounder, which is matched with a MakeSomeDNA method, which accepts the AnimalSounder interface type as an input parameter.

So in the Go language. As long as the supporting Cat and Dog instances also implement the MakeSomeDNA method, then we can consider him to be the AnimalSounder interface type:

type AnimalSounder interface { 
 	MakeDNA() 
} 

func MakeSomeDNA(animalSounder AnimalSounder) { 
 	animalSounder.MakeDNA() 
} 

func(c *Cat) MakeDNA() { 
 	fmt.Println("CatDNA") 
} 

func(c*Dog); MakeDNA() { 
 	fmt.Println(" 
MakeDNA (& 
Cat 
 	{}) 
 	MakeSomeDNA(&Dog{}) 
};

When the instances of Cat and Dog implement the constraints of the AnimalSounder interface type, it means that the conditions are met, and they are one thing in the Go language. It can be passed into the MakeSomeDNA method as an input parameter, and then polymorphic behavior can be realized according to different instances.


In daily work, a basic understanding of these concepts is enough. If it is an interview, you can focus on three major features: "encapsulation, inheritance, polymorphism" and five principles "Single Responsibility Principle (SRP), Open and Closed Principle (OCP), Liskov Substitution Principle (LSP), Dependency Inversion Principle (DIP), Interface Segregation Principle (ISP)" for in-depth understanding and explanation.

4. The difference between go language and python:

1. Example

Python is a multi-paradigm, imperative and functional programming language based on object-oriented programming. It insists on the idea that if a language behaves a certain way in some contexts, it should ideally behave similarly in all contexts. However, it is not a pure OOP language, it does not support strong encapsulation, which is one of the main principles of OOP.

Go is a procedural programming language based on the concurrent programming paradigm, which shares superficial similarities with C. In fact, Go is more like an updated version of C.

2. Typing

Python is a dynamically typed language, while Go is a statically typed language, which actually helps to catch errors at compile time, which can further reduce critical bugs later in production.

3. Concurrency

Python does not provide a built-in concurrency mechanism, while Go has a built-in concurrency mechanism.

4. Security

Python is a strongly typed language, it is compiled, thus adding a layer of safety. Go has a type assigned to each variable, thus, it provides safety. However, if any errors occur, users need to run the entire code themselves.

5. Manage memory

Go allows programmers to manage memory to a great extent. However, memory management in Python is fully automated and managed by the Python VM; it does not allow the programmer to take responsibility for memory management.

6. Library

Compared to Go, Python provides a much larger number of libraries. However, Go is still new and hasn't made much progress yet.

7. Grammar

Python's syntax uses indentation to indicate blocks of code. Go's syntax is based on opening and closing parentheses.

8. Level of detail

To achieve the same functionality, Golang code usually requires writing more characters than Python code.

 

Guess you like

Origin blog.csdn.net/2301_76484015/article/details/130503291