简体   繁体   中英

What does type do in the Go statement “type subscriber struct{…}?”

I'm new to the language Go and also have never used structs before. My understanding of structs are that they group data of different types together when it's relevant to do so. Like this example shows which comes from the book "Head First Go":

`type subscriber struct{  

    name string  
    rate float64  
    active bool  
}`

My question is how would that struct code compare to this struct code (which I made up):

var subscriber struct{
    
        name string  
        rate float64  
        active bool  
}

To me it seemed like the first struct- with the keyword type- was one you could re-use (ie. instantiate as many times as you want). So this would make it a definition then?

Then is the second subscriber struct- with the keyword var- not like a definition then. Is it just a single struct created on the fly with the var name subscriber? Meaning you can't make any more subscriber structs with this code.

I was looking at structs in C (which maybe was a mistake) and it seemed like structs were more like the first example with the keyword type? That is the struct code was always a definition that lets you create as many of those particular structs as you want. Is this accurate? Sorry I know it's technically more like 2 questions.

The code:

struct {
        name string  
        rate float64  
        active bool  
}

is a struct type literal.

The type keyword introduces a type declaration . The type statement in the question declares the named type subscriber with the specified struct type literal.

The var keyword introduces a variable declaration . The var statement in the question declares the variable subscriber with anonymous type specified by the struct type literal.

To me it seemed like the first struct- with the keyword type- was one you could re-use (ie. instantiate as many times as you want).

The key point is that a declared type has a name and the name can be used to refer to the type.

Then is the second subscriber struct- with the keyword var- not like a definition then. Is it just a single struct created on the fly with the var name subscriber?

It's a definition in the sense that it defines the variable subscriber . The variable subscriber has the anonymous type specified by the struct type literal.

Meaning you can't make any more subscriber structs with this code.

The application can use a short variable declaration to declare another variable with the same anonymous type:

 subscriber2 := subscriber

The application can declare a variable with the same type literal:

var subscriber3 struct{
    
        name string  
        rate float64  
        active bool  
}

The variables subscriber , subscriber2 and subscriber3 all have the same anonymous type.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM