About golang type creation specifications

王林
Release: 2024-02-10 12:06:09
forward
778 people have browsed it

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}
}
Copy after login

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

Futest{
        Name: "test",
    }
Copy after login

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)
Copy after login

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"} 
Copy after login

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!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!