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 use type definition in Golang communication

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

Share

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

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

Catalogue

1. Custom type

2. Type definition

2.1 define the structure

2.2 define Interfac

2.3 define other new types

2.4 define the type of function

3. Type alias

4. The difference between type definition and type alias

5. Non-local types cannot define methods

6. Use aliases when structure members are embedded

1. Custom type

There are some basic data types in the go language, such as string, integer, floating-point, Boolean and other data types. The Go language can use the type keyword to define custom types.

Type is an important and commonly used keyword in Go grammar, and type is by no means just corresponding to typedef in Cmax +. If you understand the use of type, it is easy to understand the core concepts in the go language, such as struct, interface, functions, and so on.

2. Type definition 2.1 defines the structure

Use type to define structure types:

/ / 1. Define structure / / structure definition type person struct {name string / / Note that there cannot be a comma age int} 2.2 to define the interface

Use type to define interface types:

Type USB interface {start () end ()} 2.3 defines other new types

Using type, you can also define new types.

Syntax:

Type type name Type

Sample code:

Type myint inttype mystr stringfunc main () {var i1 myint var i2 = 100i1 = 100fmt.Println (i1) / / i1 = i2 / / cannot use i2 (type int) as type myint in assignment fmt.Println (i1Poweri2) var name mystr name = "Wang er Dog" var S1 string S1 = "Li Xiaohua" fmt.Println (name) fmt.Println (S1) name = S1 / / cannot use S1 (type string) as type mystr in assignment} 2.4 defines the type of function

The Go language supports functional programming and can use high-level programming syntax. A function can be used as an argument to another function or as a return value of another function. When defining this higher-order function, if the type of the function is complex, you can use type to define the type of this function:

The return value of the func main () {res1: = fun1 () fmt.Println (res1 (10Magazine 20))} type my_fun func (int,int) (string) / / fun1 () function is the my_func type func fun1 () my_fun {fun: = func (int) string {s: = strconv.Itoa (a) + strconv.Itoa (b) return s} return fun} 3, type alias

The type alias states that TypeAlias is just an alias for Type, and essentially TypeAlias and Type are of the same type. Just like a child has a nickname or nickname when he is young, he uses a scientific name after school, and his English teacher will give him an English name, but these names all refer to him.

Type aliases are a new feature added in Go version 1.9. It is mainly used for the compatibility of types in code upgrade and migration. Code refactoring upgrades can use macros to quickly define a new piece of code in the Candlestick + language. Instead of choosing to add macros in the Go language, it will solve the most troublesome problem of changing type names in refactoring.

Type TypeAlias = Type

The code for built-in type definitions prior to Go 1.9 was written as follows:

Type byte uint8type rune int32

After Go 1.9, it becomes:

Type byte = uint8type rune = int32

This change is made in conjunction with the type alias.

4. The difference between type definition and type alias

On the surface, there is only one equal sign difference between the type alias and the type definition, and we can understand the difference between them through the following code.

/ / Type definition type NewInt int// type alias type MyInt = intfunc main () {var a NewInt var b MyInt fmt.Printf ("type of type of% T\ n", a) / / type of a:main.NewInt fmt.Printf ("type of BVA% T\ n", b) / / type of b:int}

The result shows that the type of an is main.NewInt, which represents the NewInt type defined under the main package. The type of b is int. The MyInt type only exists in the code, and there is no MyInt type when the compilation is complete.

5. Non-local types cannot define methods

Being able to name various types at will means that you can add methods for these types in your own package.

/ / the alias that defines time.Duration is MyDurationtype MyDuration = time.Duration// to add a function func (m MyDuration) EasySet (a string) {/ / cannot define new methods on non-local type time.Duration} func main () {} for MyDuration

The above code reported an error. Error message: cannot define new methods on non-local type time.Duration

Compiler hint: you cannot define a new method on a non-native type time.Duration. A non-native method refers to the package in which the code that uses time.Duration is located, that is, the main package. Because time.Duration is defined in the time package, it is used in the main package. The time.Duration package is not in the same package as the main package, so you cannot define methods for types that are not in the same package.

There are two ways to solve this problem:

Change the type alias to the type definition: type MyDuration time.Duration, that is, change the MyDuration from alias to type.

Put the alias definition of MyDuration in the time package.

6. Use aliases when structure members are embedded

What happens when a type alias is embedded as a member of a structure?

Type Person struct {name string} func (p Person) Show () {fmt.Println ("Person-- >", p.name)} / / Type alias type People = Persontype Student struct {/ / embed two structures Person People} func (p People) Show2 () {fmt.Println ("People- >" P.name)} func main () {/ / var s Student / / s.name = "Wang er Dog" / / ambiguous selector s.name s.People.name = "Li Xiaohua" s.Person.name = "Wang er Dog" / / s.Show () / / ambiguous selector s.Show s.Person.Show () s.People.Show2 () fmt.Printf ("% TMagne% T\ n", s.Personpens. People) / / main.Person,main.Person}

When accessing name directly through s, or s directly calls the Show () method, because both types have name fields and Show () methods, ambiguity occurs, proving that the essence of People is indeed the Person type.

At this point, the study on "how to use type definition in Golang communication" 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