PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, July 9, 2022

[FIXED] What (exactly) does the type keyword do in go?

 July 09, 2022     go, keyword, struct, types     No comments   

Issue

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?


Solution

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.



Answered By - hscasn
Answer Checked By - Gilberto Lyons (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing