43

I've been reading A Tour of Go to learn Go-Lang and so far it's going good.

I'm currently on the Struct Fields Lesson and here is the sample code from the right hand side:

package main

import "fmt"

type Vertex struct {
  X int
  Y int
}

func main() {
  v := Vertex{1, 2}
  v.X = 4
  fmt.Println(v.X)
}

Take a look at line 3:

type Vertex struct {

What I don't understand is, what does the type keyword do and why is it there?

Malekai
  • 4,765
  • 5
  • 25
  • 60

3 Answers3

43

The type keyword is there to create a new type. This is called type definition. The new type (in your case, Vertex) will have the same structure as the underlying type (the struct with X and Y). That line is basically saying "create a type called Vertex based on a struct of X int and Y int".

Don't confuse type definition with type aliasing. When you declare a new type, you are not just giving it a new name - it will be considered a distinct type. Take a look at type identity for more information on the subject.

hscasn
  • 773
  • 6
  • 12
  • 2
    Additionally, if you have a type `A` with a method `B`. If you define another type `C` as `type C A` you won't be able to call method `B` on type `C`. That is `var c C; c.B()` won't compile. See https://play.golang.org/p/bocCzDqu3lh – John Leidegren Apr 22 '19 at 14:12
  • 1
    Is there a way to define structs without creating a new `type`? Otherwise the syntax looks a bit redundant to me. – Jan Christoph Terasa Mar 30 '22 at 09:02
  • @JanChristophTerasa I had the exact same question. Is it possible to define a `struct` without a creating it as a named type? – EternalObserver Jun 10 '23 at 05:52
21

It's used to define a new type.

General format:
type <new_type> <existing_type or type_definition>

Common use cases:

  • Create a new type for an existing type.
    Format:
    type <new_type> <existing_type>
    e.g
    type Seq []int
  • Create a type while defining struct.
    Format:
    type <new_type> struct { /*...*/}
    e.g
    https://gobyexample.com/structs
  • Define function type, (aka. by assigning name to a function signature).
    Format:
    type <FuncName> func(<param_type_list>) <return_type>
    e.g
    type AdderFunc func(int, int) int

In your case:

It define a type named Vertex for a new struct, so that later you can refer to the struct via Vertex.

Eric
  • 22,183
  • 20
  • 145
  • 196
2

Actually type keyword is same with class topology in PHP.

With type keyword as though you create class in GO

Example type in struct

type Animal struct {
  name string //this is like property
}

func (An Animal) PrintAnimal() {
  fmt.Println(An.name) //print properties
}

func main() {
  animal_cow := Animal{ name: "Cow"} // like initiate object

  animal_cow.PrintAnimal() //access method
}

OK let's move with type string (is same for int or float)

   type Animal string
        
   // create method for class (type) animal
   func (An Animal) PrintAnimal() {
      fmt.Println(An) //print properties
   }
    
   func main(){
      animal_cow := Animal("Cow") // like initiate object
    
      animal_cow.PrintAnimal() //access method
      //Cow
   }

Difference between struct and string, int, float just in struct you can add more properties with any different data type

Opposite in string, int, float you can only have 1 properties, which created when you initiate your type (ex: animal_cow := Animal("Cow")

But, all type which build using type keyword can definitely have more than 1 method

Correct me if I am wrong

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Muhammad Ichsan
  • 193
  • 2
  • 6