• Skip to primary navigation
  • Skip to content

GitopsCentral

  • Home
  • Courses
  • Roadmap
  • About
  • Log In
  • Sign Up

Composite types: Structs

April 13, 2019 by shaik zillani

gophers
©techort.com

Structs

Struct is a collection of fields.

Syntax

type User struct {
      username string
      email string
      phone string
}

var u1 User

Initializing Structs

Struct can be initialized using new keyword which will initialize fields with 0or in below manner,

 user1 := User {username: "sam", email: "sam123@gmail.com", phone: "987654322"}

Using new keyword,

user1 := new(User)

Example

package main

import "fmt"

type User struct {
        username string
        email string
        phone string
}

func main() {
  user1 := User {username: "sam", email: "sam123@gmail.com", phone: "987654322"}
  fmt.Println(user1)
}

Output

{sam sam123@gmail.com 987654322}

golang go,  golang,  structs

© Copyright 2016-2025 gitopscentral · All Rights Reserved ·