Go’s type system

Tram Ho

Go provides a system type (type) no hierarchy (hierarchy-free type) flexibility, this enables code reuse restructuring cost minimal. It is still an evolution like OOP, but without the complexities of traditional traditions. If you’ve ever spent a week planning abstract classes and interfaces in a Java or C ++ program, you’ll appreciate the simplicity of the type system in Go.

Go developers only need to embed types to reuse functionality in a design pattern called composition . In other languages also use components (composition), but it is often closely to inheritance, this can make it complicated and difficult to use. In Go, types are combined from smaller types . Contrary to the types of models based on traditional inheritance. In addition, Go tries an interesting way of implementing interfaces that allow the programmer to model behavior rather than types . You don’t need to declare what you implement an interface in Go. The compiler determines which interfaces of your type meet. A lot of interfaces in the Go standard library is very small, it exposed (exposing) only one function. If you have ever used object oriented programming ( OOP ) like Java, C # … then you will quickly get used to it.

Go provides a few types such as int, string and also allows programmers to define the data type to store data. If you have ever known about struct in C , user defined types in Go work similarly, but can declare more methods that operate on its data. Instead of building structures inherited (Client extends extends User Entity), programmer Go build small type such as Customer and admin, then embed (embed) them into a larger pattern.

Illustration

Interfaces allow you to express the behavior of a type. If a value of a type implements an interface , that value means that a particular set of behaviors. You also don’t need to explicitly declare you are implementing an interface ; you just need to write content for that implementation. In Go, if a data type implements methods of an interface , a value of your type can be stored in a value of a certain interface type. No special declaration is required. In object oriented programming ( OOP ), normally an interface will consist of multiple methods, so you often spend a lot of time thinking ( declare a interface ) before writing code. Here is an example of an interface in Java:

Implementing this interface in Java requires you to create a class that completes all the methods defined in the User interface and explicitly declare you are implementing that interface . In contrast, the interface in Go represents a single method . You can find the example inside the Go standard library:

To write a type of Reader interface implements, you just need to implement the Read method, which receives an array of bytes and returns an integer and error

This is a complete difference from the interface in object oriented programming. Interface in Go is very small and normally it only declares one method . In practice, this makes sense to increase code reuse. You can implement a Reader interface and any type can implement it.

This article I would like to stop here. I know the article is quite heavy in theory, but I also hope that based on the keywords in the article, you can learn more by yourself to increase the understanding of Types in Go (especially the interface ).

After a series of theoretical introductions, I will also deploy a few articles with code as examples to help people have a new look. Thank you for reading my article

In addition, I have a blog to save my articles. Here is the link of the original article: https://chiasekienthuc.netlify.app/blog/golang-type-system . You guys support me =))

Share the news now

Source : Viblo