In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the Go language type alias and custom type example analysis, the article is very detailed, has a certain reference value, interested friends must read!
Go does not have the concept of classes, nor does it support object-oriented concepts such as inheritance of "classes."
Go has a higher extensibility and flexibility than object-oriented programming through built-in refit interfaces in structs.
1. Type aliases and custom types
1.1 custom type
There are some basic data types in Go, such as string, integer, floating point, Boolean and other data types. In Go, you can use the type keyword to define custom types.
A custom type defines a completely new type. We can define it based on built-in primitive types or we can define it via struct.
//define MyInt as int type
type MyInt int
By definition of the Type keyword, MyInt is a new type that has the properties of int.
1.2 type alias
Type aliases are a new feature added in Go 1.9.
TypeAlias rule: TypeAlias is simply an alias for Type, and TypeAlias is essentially the same type as Type. Like our English names, our baby names, but they all refer to one person.
type TypeAlias = type
The rune and byte we saw earlier are both type aliases, defined as follows:
type byte = uint8
type rune = int32
1.3 Difference between type definition and type alias
Type aliases and type definitions, on the surface, only one equal sign difference, we can see the difference between them through the following code.
package main
import "fmt"
type NewInt int
type MyInt = int
func main() {
var a NewInt
var b MyInt
fmt.Printf("type of a:%T\n",a)
fmt.Printf("type of b:%T\n",b)
}
Results:
type of a:main.NewInt
type of b:int
Process finished with exit code 0
The result shows that the type of a is main.NewInt, which means the NewInt type defined under the main package.
Type b is int. MyInt type will only exist in code, there will be no MyInt type when compilation is complete.
The above is all the content of this article "Example analysis of type aliases and custom types in Go language", thank you for reading! Hope to share the content to help everyone, more relevant knowledge, welcome to pay attention to the industry information channel!
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.