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

How to understand type aliases in Go

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "how to understand type aliases in Go". In daily operation, I believe many people have doubts about how to understand type aliases in Go. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "how to understand type aliases in Go". Next, please follow the editor to study!

There are three lines of code:

/ / 32-bit machine 1) var x int32 = 32.02) var y int = x 3) var z rune = x

Can they be compiled? Why?

If you ask this question during the interview, you need to think about what the interviewer wants to test you. Before you move on, I suggest you write down your answer.

01 numeric literal quantity

In the Go language, literals are untyped. What do you mean untyped? Untyped means that values can be assigned to variables or constants of a similar type. Using the above example, 32.0 is an untyped floating-point numeric quantity, so it can be assigned to any numeric related type variable (or constant). The following are legal:

Var an int64 = 32.0 var b int = 32.0 var c float32 = 32.0 var d complex64 = 32.0 var e byte = 32.0 var f rune = 32.0

So 1) in the previous question is correct.

02 different types

In the current Go version 1.16 (actually only very early versions are not), int types take up 4 bytes on 32-bit machines and 8 bytes on 64-bit machines. So, on 32-bit machines, the memory footprint and layout of int32 and int are exactly the same. However, the Go language does not do implicit type conversion. Int and int32 are different types, so 2) compilation failed in the previous question.

03 type alias

For those of you who are familiar with the C language, you can see the following definition in Go:

Type myint int

You will think that myint and int are the same, and think that myint is an alias for int. In fact, myint is a completely different type from int, except that the underlying type of myint is int, which can be cast directly, but not implicitly. There is no need to say more about this, but the focus is on type aliases.

Type aliases have been introduced since Go1.9, and are defined as follows:

AliasDecl = identifier, "=", Type.

Specific examples:

Type intalias = int

Myint is a new type, unlike int, while intalias, like int, is just an alias for int: int can be used everywhere you use intalias.

So why are type aliases introduced in Go? Russ Cox's paper Codebase Refactoring (with help from Go) introduces its background. To summarize the purpose of type aliases, there are two main points:

When refactoring project code on a large scale, especially when moving a type from one package to another, some code uses types from the new package, and some code uses types from the old package, most typically the context package. At first, the name of the context package is golang.org/x/net/context,1.7, and the standard library is introduced, so that there are two copies. Go 1.9began to ReFactor it with aliases

A large packet is allowed to be broken down into several internal packets, but the types in the packets need to be concentrated in the large packets at the upper level.

In Go, you can define aliases for any type, such as arrays, structures, pointers, functions, interfaces, Slice, Map, Channel, etc., including aliases for custom types.

Type F = func () type I = interface {}...

In addition, you can define aliases for types in other packages, such as aliases for standard library types:

Type MyReader = bufio.Reader

Some considerations about type aliases:

The alias is the same as the original type, so there cannot be two case in the switch-type structure, one is the original type and the other is the alias

Type aliases cannot be defined iteratively, for example, the following are not allowed:

Type T = struct {next * T1} type T1 = T

Because the alias is the same as the original type, share the same set of methods, regardless of whether the method is defined on the original type or alias

The exportation of an alias can be different from the original type.

You cannot add methods to other package types by defining aliases. The following behaviors are not allowed:

Type MyReader = bufio.Reader func (MyReader) AliasMethod () {fmt.Println ("This is alias method")}

Compilation error: cannot define new methods on non-local type bufio.Reader.

Going back to the beginning title 3), what is the type of rune? The definition is as follows:

Type rune = int32

Obviously, rune is an alias for int32, so 3) in the title can also be compiled.

In addition to the rune,Go built-in types, byte is an alias for uint8:

Type byte = uint8

It is important to note that before Go1.9, the alias nature of rune and byte existed and was handled by the compiler. It's just that after Go1.9, aliases can be used for other types.

At this point, the study on "how to understand type aliases in Go" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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