Home  >  Article  >  Backend Development  >  About golang type creation specifications

About golang type creation specifications

王林
王林forward
2024-02-10 12:06:09779browse

About golang type creation specifications

php editor Banana will introduce you to the golang type creation specifications. In Golang, type creation is very important, it determines the data type of the variable and its operable methods. When creating types, we need to follow some conventions to ensure the readability and maintainability of the code. This article will provide you with a detailed analysis of the specifications and best practices of Golang type creation to help you better understand and apply them. Both beginners and experienced developers can benefit from it. Let’s find out together!

Question content

Sample code

package main

import "fmt"

type ipoint int

type futest struct {
    name string
}

func main() {
    i := ipoint(1)
    fmt.println(i) //print 1

    futest := futest{
        name: "test",
    }
    fmt.println(futest) //print {test}
}

my question is: Why does the ipoint object only create ipoint(1), while the futest structure requires a more complex statmenet

Futest{
        Name: "test",
    }

Any golang specification describes it

Solution

ipoint is of int type, futest is of struct type. We can convert the integer to ipoint and assign it to a new variable named i as shown below.

i := ipoint(1)

We can create a new instance from the structure as shown below.

  futest := Futest{
        Name: "test",
  }

  // or

  futest := Futest{"test"}

  // If the struct has more than one fields,
  // We need to maintain the order of fields.
  // 
  //  Example:
  //
  //   type A struct {
  //    Number int
  //    Name string
  //   }
  // 
  //   a := A{1,"sample"} 

The above is the detailed content of About golang type creation specifications. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete