“Golang Struct” Code-Antworten

Was ist Struktur in Golang

A structure or struct in Golang is a user-defined type that allows to group/combine items of possibly different types into a single type. Any real-world entity which has some set of properties/fields can be represented as a struct.
Harendra

Golang Struct

package main

import (
	"encoding/json"
	"fmt"
	"net/http"
)

type Address struct {
	Street  string `json:"street"`
	Suite   string `json:"suite"`
	Zipcode string `json:"zipcode"`
}

type Users struct {
	Name string `json:"name"`
	Age  uint   `json:"age"`
}

func ObjectStruct() {
	// users 1 example
	var users1 struct {
		Name string `json:"name"`
		Age  uint   `json:"age"`
	}

	users1.Name = "john doe"
	users1.Age = 23

	// users 2 example
	users2 := Users{
		Name: "john doe",
		Age:  23,
	}

	// users 3 example
	var users3 Users
	users3.Name = "john doe"
	users3.Age = 23

	// users 4 example
	var users4 Users = Users{
		Name: "john doe",
		Age:  23,
	}
  
  	// users 5 example
	var users5 = struct {
		Name string `json:"name"`
		Age  uint   `json:"age"`
    }{
      Name: "john doe",
      Age: 23,
    }
  
  	// users 6 example
	users6 := struct {
		Name string `json:"name"`
		Age  uint   `json:"age"`
	}{
		Name: "john doe",
		Age:  23,
	}

	fmt.Printf("Object struct 1 %#v \n", users1)
	fmt.Printf("Object struct 2 %#v \n", users2)
	fmt.Printf("Object struct 3 %#v \n", users3)
	fmt.Printf("Object struct 4 %#v \n", users4)
    fmt.Printf("Object struct 5 %#v \n", users5)
    fmt.Printf("Object struct 6 %#v \n", users6)
}

func ArrayObjectStruct() {
	// users 1 example
	users1 := []Users{
		Users{
			Name: "john doe",
			Age:  23,
		},
	}

	// users 2 example
	var users2 []Users
	users2 = []Users{
		Users{
			Name: "john doe",
			Age:  23,
		},
	}

	// users 3 example
	var users3 []Users = []Users{
		Users{
			Name: "john doe",
			Age:  23,
		},
	}

	// users 4 example
	users4 := make([]Users, 1)
	users4 = []Users{
		Users{
			Name: "john doe",
			Age:  23,
		},
	}
  
  	// users 5 example
	var users3 []Users = []Users{
		Users{
			Name: "john doe",
			Age:  23,
		},
	}

	fmt.Printf("Array object struct 1 %#v \n", users1)
	fmt.Printf("Array object struct 2 %#v \n", users2)
	fmt.Printf("Array object struct 3 %#v \n", users3)
	fmt.Printf("Array object struct 4 %#v \n", users4)
}

func main() {
	ObjectStruct()
	fmt.Printf("\n")
	ArrayObjectStruct()
}
Restu Wahyu Saputra

Declare Go struct

type StructureName struct {
  // structure definition 
}
SAMER SAEID

Ähnliche Antworten wie “Golang Struct”

Fragen ähnlich wie “Golang Struct”

Weitere verwandte Antworten zu “Golang Struct” auf Go

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen