What is the usage scenario of Go Type?
This article mainly introduces "what is the use scene of Go Type". In the daily operation, I believe that many people have doubts about what the use scene of Go Type is. 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 doubt of "what is the use scene of Go Type?" Next, please follow the editor to study!
Go Type usage scenario
Type usage scenario
1. Define the structure
/ / define the trademark structure / / define Brand as the following structure type type Brand struct {} / / add the Show () method func (t Brand) Show () {} to the trademark structure
two。 As an alias
The code that defines built-in types prior to Go 1.9 is written as follows:
Type byte uint8type rune int32
After Go 1.9, it becomes:
Type byte = uint8type rune = int32
Distinguish between type aliases and type definitions
/ / define NewInt as int type type NewInt int// will int have an alias IntAliastype IntAlias = intfunc main () {/ / declare an as NewInt type var a NewInt / / View a type name fmt.Printf ("a type:% T\ n", a) / / declare a2 as IntAlias type var a2 IntAlias / / View a2 type name fmt.Printf ("a2 type:% T\ n" A2)} a type: main.NewInta2 type: int
Define structures in batch
Type (/ / A PrivateKeyConf is a private key config. PrivateKeyConf struct {Fingerprint string KeyFile string} / / A SignatureConf is a signature config. SignatureConf struct {Strict bool `json: ", default=false" `Expiry time.Duration `json: ", default=1h" `PrivateKeys [] PrivateKeyConf})
Single definition structure
Type PrivateKeyConf struct {Fingerprint string KeyFile string} type SignatureConf struct {Strict bool `json: ", default=false" `Expiry time.Duration `json: ", default=1h" `PrivateKeys [] PrivateKeyConf} at this point, the study of "what is the usage scenario of Go Type" is over. I hope to solve everyone's 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!