{"id":806,"date":"2026-10-07T15:00:00","date_gmt":"2026-10-07T09:30:00","guid":{"rendered":"https:\/\/smitabharat.com\/blog\/go-struct-programs\/"},"modified":"2026-11-29T01:29:33","modified_gmt":"2026-11-28T19:59:33","slug":"go-struct-programs","status":"publish","type":"post","link":"https:\/\/smitabharat.com\/blog\/go-struct-programs\/","title":{"rendered":"Go Struct Programs with Output: Methods, Embedding, JSON Tags"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Go struct programs group related data into one type, such as an employee with a name, salary and department, or an order with items and a total. Structs are how Go models real things, so you will use them in every project. In this post, you will write fourteen struct programs with their exact output, covering methods, nested structs, embedding, constructors and JSON tags, followed by the struct questions interviewers ask most.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You should know functions from our <a href=\"https:\/\/smitabharat.com\/blog\/go-function-programs\/\">Go function programs<\/a> and slices from our <a href=\"https:\/\/smitabharat.com\/blog\/go-slice-programs\/\">Go slice programs<\/a>. For the theory, read the <a href=\"https:\/\/smitabharat.com\/blog\/go-structs-beginners-guide\/\">Go structs guide for beginners<\/a> and <a href=\"https:\/\/smitabharat.com\/blog\/go-methods-receivers\/\">Go methods and receivers<\/a>. All programs were tested on Go 1.27.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What are Go Struct Programs?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Go struct programs define a struct type, which is a named group of fields. Each field has a name and a type. You then create values of that type and attach methods, which are functions tied to the struct.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Think of a struct as a form. An employee form has boxes for Name, Department and Salary. The struct type is the blank form, and each struct value is one filled-in copy.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why Go Struct Programs Matter<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Go has no classes, so structs do that job. For example, an API request body is a struct, a database row maps to a struct and a config file loads into a struct. So Go struct programs are the base for almost everything in Levels 5 and 6.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Modelling data:<\/strong> users, orders, products and invoices are all structs.<\/li>\n\n\n<li><strong>Behaviour:<\/strong> methods give each struct its own actions.<\/li>\n\n\n<li><strong>Interviews:<\/strong> value vs pointer receivers, embedding and comparison are common questions.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">How Go Struct Programs Work<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">You declare a type with <code>type Name struct { ... }<\/code>. Then you create values with a literal, read fields with a dot, and call methods with the same dot syntax. When you assign a struct, Go copies all its fields.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><a href=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-struct-programs-diagram-1.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-struct-programs-diagram-1-1024x640.png\" alt=\"Go struct programs diagram showing a struct type, a value with fields and methods\" class=\"wp-image-801\" srcset=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-struct-programs-diagram-1-1024x640.png 1024w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-struct-programs-diagram-1-300x188.png 300w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-struct-programs-diagram-1-768x480.png 768w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-struct-programs-diagram-1-1536x960.png 1536w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-struct-programs-diagram-1-2048x1280.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">A struct type defines the fields, each value holds its own data, and methods add behaviour to the type.<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The table below shows the main ways to create a struct value.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Way<\/th><th>Example<\/th><th>Notes<\/th><\/tr><\/thead><tbody><tr><td>Named fields<\/td><td>Employee{Name: &#8220;Asha&#8221;, Salary: 50000}<\/td><td>Clear and safe, the usual way<\/td><\/tr><tr><td>Positional<\/td><td>Employee{&#8220;Asha&#8221;, &#8220;IT&#8221;, 50000}<\/td><td>Breaks when fields change<\/td><\/tr><tr><td>Zero value<\/td><td>var e Employee<\/td><td>All fields at zero<\/td><\/tr><tr><td>Pointer<\/td><td>&amp;Employee{Name: &#8220;Asha&#8221;}<\/td><td>Ready for pointer methods<\/td><\/tr><tr><td>Constructor<\/td><td>NewEmployee(&#8220;Asha&#8221;, 50000)<\/td><td>Adds checks and defaults<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Basic Go Struct Programs<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">These first Go struct programs create values and read their fields.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 1: A Book, the Simplest of Go Struct Programs<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Start with the smallest possible struct. It has two fields, and you read each one with a dot.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ Book holds the title and page count of one book.\ntype Book struct {\n    Title string \/\/ the name of the book\n    Pages int    \/\/ how many pages it has\n}\n\nfunc main() {\n    \/\/ Create a Book value and fill in both fields by name.\n    book := Book{Title: \"Learn Go\", Pages: 250} \/\/ := creates a new variable\n\n    \/\/ Read each field with a dot.\n    fmt.Println(\"Title:\", book.Title)\n    fmt.Println(\"Pages:\", book.Pages)\n\n    \/\/ Change a field the same way.\n    book.Pages = 300\n    fmt.Println(\"New page count:\", book.Pages)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Title: Learn Go\nPages: 250\nNew page count: 300<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">First, <code>type Book struct<\/code> describes the shape of the data. Then <code>Book{...}<\/code> makes one real book with values. You read a field with <code>book.Title<\/code> and change it with <code>book.Pages = 300<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 2: Define and Print a Struct<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ Employee describes one person who works in a company.\ntype Employee struct {\n    Name   string\n    Dept   string \/\/ Dept is short for department\n    Salary int\n}\n\nfunc main() {\n    employee := Employee{Name: \"Asha\", Dept: \"IT\", Salary: 65000}\n    var empty Employee \/\/ no values given, so every field is at its zero value\n\n    fmt.Println(employee)          \/\/ prints only the values\n    fmt.Printf(\"%+v\\n\", employee) \/\/ %+v prints field names too\n    fmt.Println(employee.Name, \"earns\", employee.Salary)\n    fmt.Printf(\"%+v\\n\", empty)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>{Asha IT 65000}\n{Name:Asha Dept:IT Salary:65000}\nAsha earns 65000\n{Name: Dept: Salary:0}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>Println<\/code> shows only the values inside curly braces. The <code>%+v<\/code> verb also shows each field name, which helps when you debug. An empty struct value has every field at its zero value: &#8220;&#8221; for strings and 0 for ints.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 3: Student Marks with a Slice of Structs<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ Student holds a name and a list of marks.\ntype Student struct {\n    Name  string\n    Marks []int \/\/ a slice that can hold any number of marks\n}\n\n\/\/ Average returns the mean of the student's marks.\nfunc (student Student) Average() float64 {\n    \/\/ Step 1: add up all the marks.\n    total := 0\n    for _, mark := range student.Marks { \/\/ range walks over each mark; _ skips the index\n        total += mark\n    }\n    \/\/ Step 2: divide by the number of marks.\n    return float64(total) \/ float64(len(student.Marks))\n}\n\nfunc main() {\n    class := []Student{\n        {\"Ravi\", []int{78, 85, 90}},\n        {\"Meera\", []int{92, 88, 95}},\n        {\"Kiran\", []int{60, 72, 68}},\n    }\n    for _, student := range class {\n        fmt.Printf(\"%-6s %.1f\\n\", student.Name, student.Average()) \/\/ %-6s pads the name, %.1f shows one decimal\n    }\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Ravi   84.3\nMeera  91.7\nKiran  66.7<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>Average<\/code> is a method with a value receiver, written <code>(student Student)<\/code>. It adds the marks, then divides by how many there are. The method only reads the data, so working on a copy is fine.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Methods in Go Struct Programs<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In Go struct programs, methods let a struct act on its own data.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 4: A Simple Method for Rectangle Area<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Here is the easiest kind of method. It reads two fields and returns one answer.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ Rectangle stores the width and height of a shape.\ntype Rectangle struct {\n    Width  int\n    Height int\n}\n\n\/\/ Area returns width times height.\nfunc (rectangle Rectangle) Area() int {\n    return rectangle.Width * rectangle.Height\n}\n\nfunc main() {\n    room := Rectangle{Width: 4, Height: 3}\n    fmt.Println(\"Area of the room:\", room.Area()) \/\/ call the method with a dot\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Area of the room: 12<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The part <code>(rectangle Rectangle)<\/code> before the name makes <code>Area<\/code> belong to the <code>Rectangle<\/code> type. Inside the method, <code>rectangle<\/code> is the value you called it on. So <code>room.Area()<\/code> multiplies 4 by 3 and returns 12.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 5: Value vs Pointer Receivers<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ Account is a simple bank account.\ntype Account struct {\n    Owner   string\n    Balance int\n}\n\n\/\/ DepositCopy gets a copy of the account, so the change is lost.\nfunc (account Account) DepositCopy(amount int) {\n    account.Balance += amount \/\/ changes a copy only\n}\n\n\/\/ Deposit gets a pointer (*Account), so it changes the real account.\nfunc (account *Account) Deposit(amount int) {\n    account.Balance += amount \/\/ changes the real account\n}\n\nfunc main() {\n    myAccount := Account{Owner: \"Asha\", Balance: 1000}\n    myAccount.DepositCopy(500)\n    fmt.Println(myAccount.Balance) \/\/ still 1000\n    myAccount.Deposit(500)         \/\/ Go passes &amp;myAccount for you\n    fmt.Println(myAccount.Balance) \/\/ now 1500\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>1000\n1500<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">A value receiver gets a copy, so its changes are lost. A pointer receiver gets the address, so it changes the original. Go adds the <code>&amp;<\/code> for you when you call <code>myAccount.Deposit<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 6: A String Method for Nice Printing<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">When a type has a <code>String() string<\/code> method, <code>fmt<\/code> uses it to print the value.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ Product is an item in a shop.\ntype Product struct {\n    Name  string\n    Price float64\n}\n\n\/\/ String tells fmt how to print a Product.\nfunc (product Product) String() string {\n    return fmt.Sprintf(\"%s (Rs %.2f)\", product.Name, product.Price) \/\/ Sprintf builds a string instead of printing\n}\n\nfunc main() {\n    items := []Product{{\"Notebook\", 45}, {\"Pen\", 10.5}}\n    fmt.Println(items[0]) \/\/ fmt calls String() for you\n    fmt.Println(items)    \/\/ it also works for every item in a slice\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Notebook (Rs 45.00)\n[Notebook (Rs 45.00) Pen (Rs 10.50)]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You never call <code>String()<\/code> yourself. Instead, <code>fmt.Println<\/code> sees the method and uses it. This is the <code>fmt.Stringer<\/code> interface, and we cover interfaces in depth later in this level.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 7: A Constructor with Validation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Go has no constructors, so we write a <code>New...<\/code> function that returns a ready value or an error.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"errors\"\n    \"fmt\"\n)\n\n\/\/ User is a person with a name and an age.\ntype User struct {\n    Name string\n    Age  int\n}\n\n\/\/ NewUser checks the input and returns a new User or an error.\nfunc NewUser(name string, age int) (*User, error) {\n    \/\/ Step 1: the name must not be empty.\n    if name == \"\" {\n        return nil, errors.New(\"name is required\") \/\/ nil means \"no user\"\n    }\n    \/\/ Step 2: the age must be sensible.\n    if age &lt; 0 || age &gt; 130 {\n        return nil, fmt.Errorf(\"invalid age %d\", age)\n    }\n    \/\/ Step 3: all good, so return the address of a new User.\n    return &amp;User{Name: name, Age: age}, nil \/\/ &amp; gives a pointer to the new value\n}\n\nfunc main() {\n    user, err := NewUser(\"Meera\", 28) \/\/ err is the error value, nil when all is fine\n    fmt.Println(*user, err)           \/\/ *user reads the value the pointer points to\n    _, err = NewUser(\"\", 20)\n    fmt.Println(err)\n    _, err = NewUser(\"Dev\", 200)\n    fmt.Println(err)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>{Meera 28} &lt;nil&gt;\nname is required\ninvalid age 200<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>NewUser<\/code> checks the name first, then the age. If a check fails, it returns <code>nil<\/code> and an error message. Returning a pointer is common for constructors, because the caller usually changes the value later.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Nested and Embedded Go Struct Programs<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Go struct programs often hold other structs, which helps you model real data.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 8: Nested Struct for an Address<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ Address is a place where someone lives.\ntype Address struct {\n    City    string\n    Pincode string\n}\n\n\/\/ Customer has a name and a whole Address inside it.\ntype Customer struct {\n    Name    string\n    Address Address \/\/ a struct inside a struct\n}\n\nfunc main() {\n    customer := Customer{\n        Name:    \"Kiran\",\n        Address: Address{City: \"Pune\", Pincode: \"411001\"},\n    }\n    fmt.Println(customer.Name, \"lives in\", customer.Address.City) \/\/ two dots to reach the city\n    customer.Address.City = \"Mumbai\"\n    fmt.Printf(\"%+v\\n\", customer)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Kiran lives in Pune\n{Name:Kiran Address:{City:Mumbai Pincode:411001}}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>Customer<\/code> struct holds an <code>Address<\/code> field, which is itself a struct. You reach the inner field with two dots, as in <code>customer.Address.City<\/code>. Changing it works the same way.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 9: Embedding for Reuse<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">When you embed a struct without a field name, its fields and methods are promoted to the outer struct.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ Person has a name.\ntype Person struct {\n    Name string\n}\n\n\/\/ Greet returns a short hello message.\nfunc (person Person) Greet() string {\n    return \"Hi, I am \" + person.Name\n}\n\n\/\/ Manager is a Person who also leads a team.\ntype Manager struct {\n    Person   \/\/ embedded: no field name, only the type\n    TeamSize int\n}\n\nfunc main() {\n    manager := Manager{Person: Person{Name: \"Ravi\"}, TeamSize: 8}\n    fmt.Println(manager.Name)    \/\/ Name comes from Person\n    fmt.Println(manager.Greet()) \/\/ Greet also comes from Person\n    fmt.Println(manager.Person.Name, manager.TeamSize)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Ravi\nHi, I am Ravi\nRavi 8<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>manager.Name<\/code> and <code>manager.Greet()<\/code> work as if they belonged to <code>Manager<\/code>. This is composition, which Go uses instead of inheritance. See our <a href=\"https:\/\/smitabharat.com\/blog\/go-struct-embedding-examples\/\">Go struct embedding guide<\/a> for more.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 10: Anonymous Struct for Quick Data<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    \/\/ A struct with no type name, used only here.\n    point := struct {\n        X, Y int\n    }{3, 4}\n    fmt.Println(point.X*point.X + point.Y*point.Y)\n\n    \/\/ A list of test cases: an input number and the answer we expect.\n    tests := []struct {\n        input int\n        want  bool\n    }{{2, true}, {3, false}, {10, true}}\n\n    for _, test := range tests {\n        isEven := test.input%2 == 0 \/\/ % gives the remainder\n        fmt.Println(test.input, isEven == test.want)\n    }\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>25\n2 true\n3 true\n10 true<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Here <code>struct { ... }{...}<\/code> defines the shape and fills it in one go. The loop checks if each input is even, then prints whether that matches <code>want<\/code>. Anonymous structs are perfect for one-off data like this, and you will see the pattern in table-driven tests.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Practical Go Struct Programs<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The next Go struct programs show how copies and tags behave in real code.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 11: Compare and Copy Structs<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ Point is a spot on a grid.\ntype Point struct{ X, Y int }\n\nfunc main() {\n    first := Point{1, 2}\n    second := first \/\/ a full copy, not a link\n    second.X = 100  \/\/ changes only the copy\n    fmt.Println(first, second)\n    fmt.Println(first == Point{1, 2}, first == second) \/\/ == compares every field\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>{1 2} {100 2}\ntrue false<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Assigning <code>second := first<\/code> makes an independent copy, so changing <code>second<\/code> leaves <code>first<\/code> alone. Structs compare field by field when every field is comparable.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 12: Update a Struct Inside a Slice<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ Task is one item on a to-do list.\ntype Task struct {\n    Title string\n    Done  bool\n}\n\nfunc main() {\n    tasks := []Task{{\"write code\", false}, {\"test\", false}}\n\n    \/\/ Wrong way: task is a copy of each item.\n    for _, task := range tasks {\n        task.Done = true \/\/ changes a copy\n    }\n    fmt.Println(tasks)\n\n    \/\/ Right way: use the index to reach the real item.\n    for i := range tasks {\n        tasks[i].Done = true \/\/ changes the slice item\n    }\n    fmt.Println(tasks)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>[{write code false} {test false}]\n[{write code true} {test true}]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>range<\/code> gives a copy of each struct, so the first loop does nothing useful. The second loop uses the index <code>i<\/code> and writes to <code>tasks[i]<\/code> directly. That changes the real item in the slice.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 13: Struct Tags and JSON<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Tags are small notes on fields. The <code>encoding\/json<\/code> package reads them to choose JSON names.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"encoding\/json\"\n    \"fmt\"\n)\n\n\/\/ Order is a shop order that we turn into JSON.\ntype Order struct {\n    ID     int     `json:\"id\"`             \/\/ JSON key will be \"id\"\n    Amount float64 `json:\"amount\"`         \/\/ JSON key will be \"amount\"\n    Note   string  `json:\"note,omitempty\"` \/\/ skipped when empty\n    secret string  \/\/ not exported, never in JSON\n}\n\nfunc main() {\n    order := Order{ID: 101, Amount: 499.5, secret: \"x\"}\n    data, _ := json.Marshal(order) \/\/ Marshal turns the struct into JSON bytes; _ ignores the error here\n    fmt.Println(string(data))      \/\/ convert the bytes to text to print them\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>{\"id\":101,\"amount\":499.5}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>json.Marshal<\/code> reads each tag and uses it as the key name. <code>omitempty<\/code> skips the empty note. The lower case <code>secret<\/code> field is not exported, so JSON never sees it. We go further with JSON in Level 5.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 14: Struct as a Map Key<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ Cell is one square on a game board.\ntype Cell struct{ Row, Col int }\n\nfunc main() {\n    board := map[Cell]string{} \/\/ a map with Cell keys and string values\n    board[Cell{0, 0}] = \"X\"\n    board[Cell{1, 1}] = \"O\"\n    fmt.Println(board[Cell{1, 1}], len(board))\n\n    _, taken := board[Cell{2, 2}] \/\/ taken is true only if the key exists\n    fmt.Println(taken)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>O 2\nfalse<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">A struct with comparable fields can be a map key. Here each <code>Cell<\/code> holds a row and a column, which makes a neat game board. The two-value lookup tells you if a square is already taken.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Value vs Pointer Receivers in Go Struct Programs<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Choosing the receiver type is the most important design choice in Go struct programs. The diagram and table below compare the two.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><a href=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-struct-programs-diagram-2.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-struct-programs-diagram-2-1024x640.png\" alt=\"Go struct programs comparison of value receivers and pointer receivers\" class=\"wp-image-802\" srcset=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-struct-programs-diagram-2-1024x640.png 1024w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-struct-programs-diagram-2-300x188.png 300w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-struct-programs-diagram-2-768x480.png 768w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-struct-programs-diagram-2-1536x960.png 1536w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-struct-programs-diagram-2-2048x1280.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">Value receivers work on a copy, while pointer receivers change the original struct.<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Feature<\/th><th>Value receiver (s T)<\/th><th>Pointer receiver (s *T)<\/th><\/tr><\/thead><tbody><tr><td>Gets<\/td><td>A copy<\/td><td>The address<\/td><\/tr><tr><td>Can change fields<\/td><td>Only the copy<\/td><td>Yes, the original<\/td><\/tr><tr><td>Cost for big structs<\/td><td>Copies all fields<\/td><td>Cheap<\/td><\/tr><tr><td>Called on nil<\/td><td>No<\/td><td>Yes, if handled<\/td><\/tr><tr><td>Use when<\/td><td>Small, read-only types<\/td><td>You change data or the type is big<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">A common rule is this: if any method needs a pointer receiver, use pointer receivers for all methods of that type, so the type behaves the same everywhere.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Common Mistakes in Go Struct Programs<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Changing a copy in range:<\/strong> <code>for _, task := range tasks<\/code> gives copies, so use the index.<\/li>\n\n\n<li><strong>Value receiver for updates:<\/strong> changes vanish, so use <code>*T<\/code> when a method changes fields.<\/li>\n\n\n<li><strong>Positional literals:<\/strong> adding a field breaks <code>Employee{\"Asha\", \"IT\", 50000}<\/code>, so use field names.<\/li>\n\n\n<li><strong>Lower case fields and JSON:<\/strong> unexported fields are skipped by <code>encoding\/json<\/code>.<\/li>\n\n\n<li><strong>Comparing structs with slices:<\/strong> a struct with a slice or map field cannot use <code>==<\/code>.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Best Practices for Go Struct Programs<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Use named fields<\/strong> in every struct literal.<\/li>\n\n\n<li><strong>Write New functions<\/strong> that check input and set defaults.<\/li>\n\n\n<li><strong>Keep receivers consistent<\/strong>, all value or all pointer for one type.<\/li>\n\n\n<li><strong>Add a String method<\/strong> for types you print often.<\/li>\n\n\n<li><strong>Prefer embedding to deep nesting<\/strong> when a type should reuse another type&#8217;s methods.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Go Struct Programs Interview Questions and Answers<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">These Go struct programs test rules that come up in almost every interview. Each program is complete, with real output from Go 1.27.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Struct Basics Questions<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q1. Does Go have classes and inheritance?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">No. Go has structs for data and methods for behaviour. Instead of inheritance, it uses embedding (composition) and interfaces.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q2. Why does this program not compile?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ Team has a name and a slice of member names.\ntype Team struct {\n    Name    string\n    Members []string\n}\n\nfunc main() {\n    teamA := Team{\"A\", []string{\"x\"}}\n    teamB := Team{\"A\", []string{\"x\"}}\n    fmt.Println(teamA == teamB) \/\/ not allowed: Members is a slice\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>invalid operation: teamA == teamB (struct containing []string cannot be compared)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">A struct can use <code>==<\/code> only when every field is comparable. Slices are not, so compare such structs with your own function or <code>reflect.DeepEqual<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q3. What does this program print?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n    \"unsafe\"\n)\n\n\/\/ Loose puts a big field between two small ones.\ntype Loose struct {\n    Active bool\n    Total  int64\n    Valid  bool\n}\n\n\/\/ Tight puts the big field first.\ntype Tight struct {\n    Total  int64\n    Active bool\n    Valid  bool\n}\n\nfunc main() {\n    \/\/ unsafe.Sizeof returns how many bytes a value takes in memory.\n    fmt.Println(unsafe.Sizeof(Loose{}), unsafe.Sizeof(Tight{}), unsafe.Sizeof(struct{}{}))\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>24 16 0<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Go adds padding so the <code>int64<\/code> lines up in memory. Putting large fields first saves space. An empty struct takes zero bytes, which is why <code>map[string]struct{}<\/code> is used for sets.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q4. What is the zero value of a struct?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It is a struct where every field holds its own zero value. A zero struct is ready to use, which is why many Go types work without a constructor.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Method Questions<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q5. What does this program print?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ Counter keeps a running count.\ntype Counter struct{ count int }\n\n\/\/ Increment adds one to the count (pointer receiver, so it changes the original).\nfunc (counter *Counter) Increment() { counter.count++ }\n\n\/\/ Value returns the current count (value receiver, read only).\nfunc (counter Counter) Value() int { return counter.count }\n\nfunc main() {\n    var counter Counter\n    counter.Increment()\n    counter.Increment()\n    pointer := &amp;counter \/\/ pointer holds the address of counter\n    pointer.Increment()\n    fmt.Println(counter.Value(), pointer.Value())\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>3 3<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Go converts between <code>counter<\/code> and <code>&amp;counter<\/code> automatically when you call methods on a variable. Both names refer to the same counter, so both print 3.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q6. What does this program print?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ Box holds a list of items.\ntype Box struct{ Items []string }\n\n\/\/ Add has a value receiver, so it works on a copy of the box.\nfunc (box Box) Add(item string) {\n    box.Items = append(box.Items, item) \/\/ append adds to the copy only\n}\n\nfunc main() {\n    box := Box{}\n    box.Add(\"pen\")\n    fmt.Println(len(box.Items))\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>0<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>Add<\/code> has a value receiver, so it appends to a copy of the struct. The original <code>box.Items<\/code> stays empty. A pointer receiver fixes this.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q7. What happens when two embedded types have the same field?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ Engine and Wheel both have a field called ID.\ntype Engine struct{ ID int }\ntype Wheel struct{ ID int }\n\n\/\/ Car embeds both of them.\ntype Car struct {\n    Engine\n    Wheel\n}\n\nfunc main() {\n    car := Car{Engine{1}, Wheel{2}}\n    fmt.Println(car.Engine.ID, car.Wheel.ID) \/\/ name the embedded type to pick one ID\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>1 2<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Writing <code>car.ID<\/code> would be a compile error, because Go cannot tell which <code>ID<\/code> you mean. You must name the embedded type, as in <code>car.Engine.ID<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Output Questions<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q8. What does this program print?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ Point is a spot on a grid.\ntype Point struct{ X, Y int }\n\n\/\/ move gets a copy of the point.\nfunc move(point Point) { point.X = 99 }\n\n\/\/ movePtr gets the address of the point, so it changes the original.\nfunc movePtr(point *Point) { point.X = 99 }\n\nfunc main() {\n    point := Point{1, 1}\n    move(point)\n    fmt.Println(point) \/\/ unchanged\n    movePtr(&amp;point)    \/\/ pass the address with &amp;\n    fmt.Println(point) \/\/ changed\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>{1 1}\n{99 1}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>move<\/code> works on a copy, so the first print still shows <code>{1 1}<\/code>. <code>movePtr<\/code> gets the address, so it changes the real point to <code>{99 1}<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q9. What does this program print?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ Config holds server settings.\ntype Config struct {\n    Port int\n    Host string\n}\n\nfunc main() {\n    config := &amp;Config{Port: 8080} \/\/ config is a pointer to a Config\n    fmt.Printf(\"%+v\\n\", config)   \/\/ printing the pointer\n    fmt.Printf(\"%v\\n\", *config)   \/\/ printing the value it points to\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&amp;{Port:8080 Host:}\n{8080 }<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Printing a pointer to a struct shows <code>&amp;<\/code> in front. Printing <code>*config<\/code> shows the value itself.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q10. Can a struct have a method and a field with the same name?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">No. Go reports a compile error, because <code>x.Name<\/code> would be ambiguous. Rename one of them.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q11. What are exported and unexported fields?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Fields that start with a capital letter are exported and can be used from other packages. Lower case fields are private to their package, and packages like <code>encoding\/json<\/code> cannot see them.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q12. What does this program print?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ Pair is a key with a number value.\ntype Pair struct {\n    Key   string\n    Value int\n}\n\nfunc main() {\n    pairs := []*Pair{{\"a\", 1}, {\"b\", 2}} \/\/ a slice of pointers to Pair\n    for _, pair := range pairs {\n        pair.Value *= 10 \/\/ pair is a copy of the pointer, but it points to the real struct\n    }\n    fmt.Println(*pairs[0], *pairs[1])\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>{a 10} {b 20}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This time <code>pair<\/code> is a copy of a pointer, so it still points to the real struct. That is why the change sticks, unlike Program 12.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Practice Task<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A bank account is a classic among Go struct programs. Create a <code>BankAccount<\/code> struct with <code>Owner<\/code> and <code>Balance<\/code> fields, and write <code>Deposit<\/code>, <code>Withdraw<\/code> and <code>String<\/code> methods. <code>Withdraw<\/code> should return an error if the balance is too low. Hint: use pointer receivers for the two methods that change the balance.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Official Resources<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/go.dev\/tour\/moretypes\/2\">A Tour of Go: Structs<\/a><\/li>\n\n\n<li><a href=\"https:\/\/go.dev\/tour\/methods\/1\">A Tour of Go: Methods<\/a><\/li>\n\n\n<li><a href=\"https:\/\/go.dev\/ref\/spec#Struct_types\">Go spec: struct types<\/a><\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Quick Recap<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Go struct programs group related fields into one named type.<\/li>\n\n\n<li>Methods with value receivers work on copies, and pointer receivers change the original.<\/li>\n\n\n<li>Embedding promotes fields and methods, which Go uses instead of inheritance.<\/li>\n\n\n<li>Structs compare with <code>==<\/code> only when every field is comparable.<\/li>\n\n\n<li>Struct tags such as <code>json:\"id\"<\/code> control how packages like <code>encoding\/json<\/code> see fields.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">What&#8217;s Next?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Next, we look closely at the addresses behind pointer receivers. Read <a href=\"https:\/\/smitabharat.com\/blog\/go-pointer-programs\/\">Go pointer programs with output<\/a>, with swap, linked updates and nil checks. For interview practice, see the <a href=\"https:\/\/smitabharat.com\/blog\/go-structs-interview-questions-part-7\/\">Go structs interview questions<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Go struct programs group related data into one type, such as an employee with a name, salary and department, or an order with items&hellip;<\/p>\n","protected":false},"author":1,"featured_media":803,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_swapz_level":"","_swapz_series_part":0,"_swapz_hide_toc":false,"_swapz_hide_featured":false,"_swapz_description":"","footnotes":""},"categories":[106],"tags":[96,95,11,104,31],"swapz_series":[],"class_list":["post-806","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-go-programs","tag-beginner","tag-go-interview-questions","tag-go-lang","tag-go-programs","tag-go-structs"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Go Struct Programs: Methods, Embedding, JSON<\/title>\n<meta name=\"description\" content=\"Go struct programs with output: methods, value and pointer receivers, constructors, embedding and JSON tags, plus struct interview questions.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/smitabharat.com\/blog\/go-struct-programs\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Go Struct Programs: Methods, Embedding, JSON\" \/>\n<meta property=\"og:description\" content=\"Go struct programs with output: methods, value and pointer receivers, constructors, embedding and JSON tags, plus struct interview questions.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/smitabharat.com\/blog\/go-struct-programs\/\" \/>\n<meta property=\"og:site_name\" content=\"Smita Bharat - Blog\" \/>\n<meta property=\"article:published_time\" content=\"2026-10-07T09:30:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-11-28T19:59:33+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-struct-programs-featured.png\" \/>\n\t<meta property=\"og:image:width\" content=\"2400\" \/>\n\t<meta property=\"og:image:height\" content=\"1260\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Smita\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Smita\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"18 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-struct-programs\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-struct-programs\\\/\"},\"author\":{\"name\":\"Smita\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\"},\"headline\":\"Go Struct Programs with Output: Methods, Embedding, JSON Tags\",\"datePublished\":\"2026-10-07T09:30:00+00:00\",\"dateModified\":\"2026-11-28T19:59:33+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-struct-programs\\\/\"},\"wordCount\":1839,\"publisher\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\"},\"image\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-struct-programs\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-struct-programs-featured.png\",\"keywords\":[\"Beginner\",\"Go Interview Questions\",\"Go Lang\",\"Go Programs\",\"Go Structs\"],\"articleSection\":[\"Go Programs\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-struct-programs\\\/\",\"url\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-struct-programs\\\/\",\"name\":\"Go Struct Programs: Methods, Embedding, JSON\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-struct-programs\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-struct-programs\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-struct-programs-featured.png\",\"datePublished\":\"2026-10-07T09:30:00+00:00\",\"dateModified\":\"2026-11-28T19:59:33+00:00\",\"description\":\"Go struct programs with output: methods, value and pointer receivers, constructors, embedding and JSON tags, plus struct interview questions.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-struct-programs\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-struct-programs\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-struct-programs\\\/#primaryimage\",\"url\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-struct-programs-featured.png\",\"contentUrl\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-struct-programs-featured.png\",\"width\":2400,\"height\":1260,\"caption\":\"Go Struct Programs featured image: struct fields, methods and JSON output\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-struct-programs\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Go Struct Programs with Output: Methods, Embedding, JSON Tags\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/\",\"name\":\"Smita Bharat Blog\",\"description\":\"Golang &amp; Backend Development Tutorials and Interview Questions\",\"publisher\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\"},\"alternateName\":\"Smita Bharat\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\",\"name\":\"Smita\",\"logo\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/smitabharat.com\\\/blog\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Go Struct Programs: Methods, Embedding, JSON","description":"Go struct programs with output: methods, value and pointer receivers, constructors, embedding and JSON tags, plus struct interview questions.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/smitabharat.com\/blog\/go-struct-programs\/","og_locale":"en_US","og_type":"article","og_title":"Go Struct Programs: Methods, Embedding, JSON","og_description":"Go struct programs with output: methods, value and pointer receivers, constructors, embedding and JSON tags, plus struct interview questions.","og_url":"https:\/\/smitabharat.com\/blog\/go-struct-programs\/","og_site_name":"Smita Bharat - Blog","article_published_time":"2026-10-07T09:30:00+00:00","article_modified_time":"2026-11-28T19:59:33+00:00","og_image":[{"width":2400,"height":1260,"url":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-struct-programs-featured.png","type":"image\/png"}],"author":"Smita","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Smita","Est. reading time":"18 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/smitabharat.com\/blog\/go-struct-programs\/#article","isPartOf":{"@id":"https:\/\/smitabharat.com\/blog\/go-struct-programs\/"},"author":{"name":"Smita","@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339"},"headline":"Go Struct Programs with Output: Methods, Embedding, JSON Tags","datePublished":"2026-10-07T09:30:00+00:00","dateModified":"2026-11-28T19:59:33+00:00","mainEntityOfPage":{"@id":"https:\/\/smitabharat.com\/blog\/go-struct-programs\/"},"wordCount":1839,"publisher":{"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339"},"image":{"@id":"https:\/\/smitabharat.com\/blog\/go-struct-programs\/#primaryimage"},"thumbnailUrl":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-struct-programs-featured.png","keywords":["Beginner","Go Interview Questions","Go Lang","Go Programs","Go Structs"],"articleSection":["Go Programs"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/smitabharat.com\/blog\/go-struct-programs\/","url":"https:\/\/smitabharat.com\/blog\/go-struct-programs\/","name":"Go Struct Programs: Methods, Embedding, JSON","isPartOf":{"@id":"https:\/\/smitabharat.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/smitabharat.com\/blog\/go-struct-programs\/#primaryimage"},"image":{"@id":"https:\/\/smitabharat.com\/blog\/go-struct-programs\/#primaryimage"},"thumbnailUrl":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-struct-programs-featured.png","datePublished":"2026-10-07T09:30:00+00:00","dateModified":"2026-11-28T19:59:33+00:00","description":"Go struct programs with output: methods, value and pointer receivers, constructors, embedding and JSON tags, plus struct interview questions.","breadcrumb":{"@id":"https:\/\/smitabharat.com\/blog\/go-struct-programs\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/smitabharat.com\/blog\/go-struct-programs\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/smitabharat.com\/blog\/go-struct-programs\/#primaryimage","url":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-struct-programs-featured.png","contentUrl":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-struct-programs-featured.png","width":2400,"height":1260,"caption":"Go Struct Programs featured image: struct fields, methods and JSON output"},{"@type":"BreadcrumbList","@id":"https:\/\/smitabharat.com\/blog\/go-struct-programs\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/smitabharat.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Go Struct Programs with Output: Methods, Embedding, JSON Tags"}]},{"@type":"WebSite","@id":"https:\/\/smitabharat.com\/blog\/#website","url":"https:\/\/smitabharat.com\/blog\/","name":"Smita Bharat Blog","description":"Golang &amp; Backend Development Tutorials and Interview Questions","publisher":{"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339"},"alternateName":"Smita Bharat","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/smitabharat.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339","name":"Smita","logo":{"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/image\/"},"sameAs":["https:\/\/smitabharat.com\/blog"]}]}},"_links":{"self":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts\/806","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/comments?post=806"}],"version-history":[{"count":1,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts\/806\/revisions"}],"predecessor-version":[{"id":887,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts\/806\/revisions\/887"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/media\/803"}],"wp:attachment":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/media?parent=806"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/categories?post=806"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/tags?post=806"},{"taxonomy":"swapz_series","embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/swapz_series?post=806"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}