{"id":807,"date":"2026-10-07T17:00:00","date_gmt":"2026-10-07T11:30:00","guid":{"rendered":"https:\/\/smitabharat.com\/blog\/go-pointer-programs\/"},"modified":"2026-11-29T01:29:32","modified_gmt":"2026-11-28T19:59:32","slug":"go-pointer-programs","status":"publish","type":"post","link":"https:\/\/smitabharat.com\/blog\/go-pointer-programs\/","title":{"rendered":"Go Pointer Programs with Output: Swap, nil Checks and new()"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Go pointer programs work with memory addresses, which let a function change a value that lives somewhere else. Pointers sound scary, but in Go they are safe and simple: there is no pointer arithmetic, and the garbage collector frees memory for you. In this post, you will write fourteen pointer programs with their exact output, and then practice the pointer questions that test your understanding of copies, nil and addresses.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You already met pointer receivers in our <a href=\"https:\/\/smitabharat.com\/blog\/go-struct-programs\/\">Go struct programs<\/a>. For the theory, read <a href=\"https:\/\/smitabharat.com\/blog\/go-pointers-explained\/\">Go pointers explained simply<\/a>. Our <a href=\"https:\/\/smitabharat.com\/blog\/go-function-programs\/\">Go function programs<\/a> also showed that Go passes copies, which is why pointers matter. All programs were tested on Go 1.27.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What are Go Pointer Programs?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Go pointer programs use a pointer, which is a variable that stores the memory address of another variable. The <code>&amp;<\/code> operator gives you the address of a value, and the <code>*<\/code> operator follows the address to read or change the value.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Think of a house and its postal address. The house is the value. The address on a card is the pointer. If you give a friend the card, they can visit the real house and paint the door, instead of painting a copy.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why Go Pointer Programs Matter<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Go copies every argument, so Go pointer programs exist for a good reason. Without pointers, a function could never change your variable. Pointers also avoid copying big structs and let you show &#8220;no value&#8221; with nil.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Changing data:<\/strong> update a struct or counter from another function.<\/li>\n\n\n<li><strong>Speed:<\/strong> pass an 8-byte address instead of copying a large struct.<\/li>\n\n\n<li><strong>Optional values:<\/strong> a <code>*int<\/code> can be nil, which means &#8220;not set&#8221;, while an <code>int<\/code> is always a number.<\/li>\n\n\n<li><strong>Interviews:<\/strong> nil pointer panics and swap programs are common questions.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">How Go Pointer Programs Work<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">When you write <code>p := &amp;x<\/code>, Go stores the address of <code>x<\/code> in <code>p<\/code>. Reading <code>*p<\/code> follows that address and gives you the value of <code>x<\/code>. Writing <code>*p = 10<\/code> changes <code>x<\/code> itself, because both point to the same memory.<\/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-pointer-programs-diagram-1.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-pointer-programs-diagram-1-1024x640.png\" alt=\"Go pointer programs diagram showing a variable, its address and a pointer that points to it\" class=\"wp-image-795\" srcset=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-pointer-programs-diagram-1-1024x640.png 1024w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-pointer-programs-diagram-1-300x188.png 300w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-pointer-programs-diagram-1-768x480.png 768w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-pointer-programs-diagram-1-1536x960.png 1536w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-pointer-programs-diagram-1-2048x1280.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">The pointer p stores the address of x, so *p reads and changes the same memory as x.<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The table below sums up the pointer syntax.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Syntax<\/th><th>Meaning<\/th><th>Example<\/th><\/tr><\/thead><tbody><tr><td>&amp;x<\/td><td>Address of x<\/td><td>p := &amp;x<\/td><\/tr><tr><td>*T<\/td><td>Type &#8220;pointer to T&#8221;<\/td><td>var p *int<\/td><\/tr><tr><td>*p<\/td><td>Value at the address<\/td><td>fmt.Println(*p)<\/td><\/tr><tr><td>*p = v<\/td><td>Change the value<\/td><td>*p = 42<\/td><\/tr><tr><td>new(T)<\/td><td>Make a zero T, return its address<\/td><td>p := new(int)<\/td><\/tr><tr><td>nil<\/td><td>Pointer that points nowhere<\/td><td>if p == nil { }<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Basic Go Pointer Programs<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">These first Go pointer programs show the two operators in action.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 1: Address and Value<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">This program uses the same names as the diagram above: <code>x<\/code> is the value and <code>p<\/code> is the pointer.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    x := 25   \/\/ x is a normal int variable\n    p := &amp;x   \/\/ &amp;x is the address of x, so p points to x\n    fmt.Println(\"x =\", x)\n    fmt.Println(\"*p =\", *p) \/\/ *p follows the address and reads the value\n\n    *p = 40 \/\/ change the value at that address, which is x\n    fmt.Println(\"x after *p = 40:\", x)\n    fmt.Println(p == &amp;x) \/\/ p holds exactly the address of x\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>x = 25\n*p = 25\nx after *p = 40: 40\ntrue<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">First, <code>p := &amp;x<\/code> stores the address of <code>x<\/code> in <code>p<\/code>. Then <code>*p = 40<\/code> writes through that address, so <code>x<\/code> becomes 40. We do not print the address itself, because it changes on every run.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 2: Swap Two Numbers with Pointers<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ swap gets the addresses of two ints and swaps the values stored there.\nfunc swap(first, second *int) { \/\/ *int means \"pointer to an int\"\n    *first, *second = *second, *first \/\/ a plain swap: both right side values are read first\n}\n\nfunc main() {\n    apples, oranges := 7, 99\n    swap(&amp;apples, &amp;oranges) \/\/ send the addresses, not copies\n    fmt.Println(apples, oranges)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>99 7<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>main<\/code> sends the addresses of both variables with <code>&amp;<\/code>. Inside <code>swap<\/code>, the <code>*<\/code> follows each address and swaps the real values. Without pointers, <code>swap<\/code> would only swap its own copies.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 3: Increment a Counter from a Function<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ incrementCopy gets a copy, so the caller sees no change.\nfunc incrementCopy(count int) { count++ }\n\n\/\/ increment gets a pointer, so it changes the caller's variable.\nfunc increment(count *int) { *count++ } \/\/ add one to the value at the address\n\nfunc main() {\n    visits := 0\n    incrementCopy(visits)\n    fmt.Println(visits) \/\/ still 0\n    increment(&amp;visits)\n    increment(&amp;visits)\n    fmt.Println(visits) \/\/ now 2\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>0\n2<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>*count++<\/code> means &#8220;add one to the value at count&#8221;. The copy version changes only its own <code>count<\/code>, so it has no effect on <code>visits<\/code>. The pointer version changes <code>visits<\/code> twice.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 4: new() for a Zero Value Pointer<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    number := new(int) \/\/ new(int) makes a zero int and returns its address\n    fmt.Println(*number)\n    *number = 15\n    fmt.Println(*number)\n\n    text := new(string) \/\/ a pointer to an empty string\n    fmt.Printf(\"%q\\n\", *text) \/\/ %q prints the string with quotes\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>0\n15\n\"\"<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>new(T)<\/code> creates a zero value of type T and returns its address. So <code>*number<\/code> starts at 0 and <code>*text<\/code> starts as an empty string. It is the same as writing <code>var value T<\/code> and then taking <code>&amp;value<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Pointers to Structs in Go Pointer Programs<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Most real Go pointer programs use pointers to structs.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 5: A Simple Pointer to a Struct<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Start here if structs and pointers together feel new. One pointer, one change, one print.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ Book holds a title and a price.\ntype Book struct {\n    Title string\n    Price int\n}\n\nfunc main() {\n    book := Book{Title: \"Go Basics\", Price: 300}\n    bookPointer := &amp;book \/\/ bookPointer points to book\n\n    bookPointer.Price = 250 \/\/ Go follows the pointer for you\n    fmt.Println(book.Price) \/\/ the original book changed\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>250<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>bookPointer<\/code> holds the address of <code>book<\/code>. When you write <code>bookPointer.Price = 250<\/code>, Go goes to that address and changes the real book. That is why <code>book.Price<\/code> prints 250.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 6: Change a Struct through a Pointer<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ Player is a game player with a score.\ntype Player struct {\n    Name  string\n    Score int\n}\n\n\/\/ addPoints adds points to the player that the pointer points to.\nfunc addPoints(player *Player, points int) {\n    player.Score += points \/\/ same as (*player).Score += points\n}\n\nfunc main() {\n    asha := Player{\"Asha\", 10}\n    addPoints(&amp;asha, 25)\n    fmt.Println(asha)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>{Asha 35}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>main<\/code> passes <code>&amp;asha<\/code>, so <code>addPoints<\/code> works on the real player. Go lets you write <code>player.Score<\/code> instead of <code>(*player).Score<\/code>. It follows the pointer for you.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 7: Return a Pointer from a Function<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ Order is a shop order with a status.\ntype Order struct {\n    ID     int\n    Status string\n}\n\n\/\/ newOrder builds an order and returns its address.\nfunc newOrder(id int) *Order {\n    order := Order{ID: id, Status: \"created\"}\n    return &amp;order \/\/ safe in Go\n}\n\nfunc main() {\n    order := newOrder(501)\n    order.Status = \"shipped\" \/\/ change it through the pointer\n    fmt.Println(*order)      \/\/ *order prints the value, not the address\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>{501 shipped}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In C, returning the address of a local variable is a bug. In Go, it is safe, because the compiler moves <code>order<\/code> to the heap when its address escapes the function. So the caller can keep using it.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 8: Pointers in a Slice of Structs<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ Item is a product with a stock count.\ntype Item struct {\n    Name  string\n    Stock int\n}\n\nfunc main() {\n    items := []*Item{{\"pen\", 5}, {\"book\", 0}, {\"bag\", 2}} \/\/ a slice of pointers to Item\n\n    \/\/ Step 1: restock any item that has run out.\n    for _, item := range items {\n        if item.Stock == 0 {\n            item.Stock = 10 \/\/ item points to the real Item\n        }\n    }\n\n    \/\/ Step 2: print every item.\n    for _, item := range items {\n        fmt.Println(item.Name, item.Stock)\n    }\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>pen 5\nbook 10\nbag 2<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>item<\/code> is a copy of each pointer, but every copy still points to the real item. So the restock change sticks, and the book now shows 10.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Nil and Optional Go Pointer Programs<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">These Go pointer programs show how nil helps you model missing data.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 9: A New Pointer Starts as nil<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Before you use nil checks, see what an empty pointer looks like.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    var pointer *int \/\/ declared but not given an address yet\n    fmt.Println(pointer == nil, pointer) \/\/ nil means \"points to nothing\"\n\n    score := 7\n    pointer = &amp;score \/\/ now it points to score\n    fmt.Println(pointer == nil, *pointer)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>true &lt;nil&gt;\nfalse 7<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">A pointer you declare without a value is <code>nil<\/code>, the zero value for pointers. After <code>pointer = &amp;score<\/code>, it holds a real address. Then <code>*pointer<\/code> safely reads 7.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 10: Check for nil Before Using a Pointer<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ Profile holds a short bio.\ntype Profile struct{ Bio string }\n\n\/\/ bioOf returns the bio, or a message when there is no profile.\nfunc bioOf(profile *Profile) string {\n    if profile == nil {\n        return \"(no profile)\"\n    }\n    return profile.Bio\n}\n\nfunc main() {\n    var missing *Profile \/\/ nil, points to nothing\n    found := &amp;Profile{Bio: \"Go developer\"}\n    fmt.Println(bioOf(missing))\n    fmt.Println(bioOf(found))\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>(no profile)\nGo developer<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">A nil pointer points to nothing. Reading <code>profile.Bio<\/code> on a nil pointer would panic, so <code>bioOf<\/code> checks first. It returns a friendly message instead of crashing.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 11: Optional Value with *int<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ Filter has an optional minimum age.\ntype Filter struct {\n    MinAge *int \/\/ nil means \"no filter\"\n}\n\n\/\/ describe turns the filter into a short sentence.\nfunc describe(filter Filter) string {\n    if filter.MinAge == nil {\n        return \"all ages\"\n    }\n    return fmt.Sprintf(\"age %d and above\", *filter.MinAge)\n}\n\nfunc main() {\n    zero := 0\n    eighteen := 18\n    fmt.Println(describe(Filter{}))                  \/\/ MinAge is nil\n    fmt.Println(describe(Filter{MinAge: &amp;zero}))     \/\/ MinAge points to 0\n    fmt.Println(describe(Filter{MinAge: &amp;eighteen})) \/\/ MinAge points to 18\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>all ages\nage 0 and above\nage 18 and above<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">A plain <code>int<\/code> cannot tell &#8220;not set&#8221; from 0. A <code>*int<\/code> can, because nil means &#8220;not set&#8221; and a pointer to 0 means &#8220;zero&#8221;. That is why APIs and JSON structs often use pointers for optional fields.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 12: Pointer to a Pointer<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    number := 5\n    pointer := &amp;number           \/\/ pointer holds the address of number\n    pointerToPointer := &amp;pointer \/\/ this holds the address of pointer\n    **pointerToPointer = 50      \/\/ follow two addresses, then change number\n    fmt.Println(number, *pointer, **pointerToPointer)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>50 50 50<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>pointerToPointer<\/code> holds the address of <code>pointer<\/code>, which holds the address of <code>number<\/code>. Two stars follow both addresses and reach <code>number<\/code>. You rarely need this in Go, but interviewers like to ask about it.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 13: Pointer to an Array<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ doubleAll doubles every number in the array the pointer points to.\nfunc doubleAll(numbers *[4]int) {\n    for i := range numbers { \/\/ range works through the pointer\n        numbers[i] *= 2\n    }\n}\n\nfunc main() {\n    numbers := [4]int{1, 2, 3, 4}\n    doubleAll(&amp;numbers)\n    fmt.Println(numbers)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>[2 4 6 8]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Arrays are copied when passed, so we send a pointer. The loop then changes the real array. With a slice, you would not need a pointer, because a slice already shares its array.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 14: Methods with a nil Receiver<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ Node is one box in a linked list. Next points to the following box.\ntype Node struct {\n    Value int\n    Next  *Node \/\/ nil when this is the last node\n}\n\n\/\/ Len counts the nodes from this one to the end.\nfunc (node *Node) Len() int {\n    if node == nil {\n        return 0 \/\/ an empty list has length 0\n    }\n    return 1 + node.Next.Len() \/\/ count this node, then the rest\n}\n\nfunc main() {\n    third := &amp;Node{Value: 3, Next: nil}\n    second := &amp;Node{Value: 2, Next: third}\n    list := &amp;Node{Value: 1, Next: second}\n    var empty *Node \/\/ a nil list\n    fmt.Println(list.Len(), empty.Len())\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>3 0<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>Len<\/code> counts one for the current node, then asks the next node for its length. At the end, <code>node.Next<\/code> is nil, and <code>Len<\/code> returns 0 instead of panicking. A pointer method can run on a nil receiver, as long as it checks for nil.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Value vs Pointer in Go Pointer Programs<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Most Go pointer programs come down to one choice. The diagram and table below compare passing a value with passing a pointer.<\/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-pointer-programs-diagram-2.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-pointer-programs-diagram-2-1024x640.png\" alt=\"Go pointer programs comparison of passing a value and passing a pointer\" class=\"wp-image-796\" srcset=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-pointer-programs-diagram-2-1024x640.png 1024w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-pointer-programs-diagram-2-300x188.png 300w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-pointer-programs-diagram-2-768x480.png 768w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-pointer-programs-diagram-2-1536x960.png 1536w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-pointer-programs-diagram-2-2048x1280.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">Passing a value copies the data, while passing a pointer shares the original so changes are visible.<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Feature<\/th><th>Pass a value<\/th><th>Pass a pointer<\/th><\/tr><\/thead><tbody><tr><td>What is copied<\/td><td>The whole value<\/td><td>An 8-byte address<\/td><\/tr><tr><td>Function can change caller&#8217;s data<\/td><td>No<\/td><td>Yes<\/td><\/tr><tr><td>Can be nil<\/td><td>No<\/td><td>Yes, check before use<\/td><\/tr><tr><td>Safe from accidental changes<\/td><td>Yes<\/td><td>No<\/td><\/tr><tr><td>Best for<\/td><td>Small, read-only data<\/td><td>Big structs or updates<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">In short, pass values by default for small types. Use pointers when the function must change the data, or when the data is large.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Common Mistakes in Go Pointer Programs<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Dereferencing nil:<\/strong> <code>var p *int; *p = 1<\/code> panics, so check <code>p != nil<\/code> first.<\/li>\n\n\n<li><strong>Pointers to everything:<\/strong> small values like ints and short structs are fine to copy.<\/li>\n\n\n<li><strong>Pointers to maps and slices:<\/strong> you rarely need <code>*map<\/code> or <code>*[]T<\/code>, because they already share data. The exception is when a function must reassign the slice itself.<\/li>\n\n\n<li><strong>Expecting pointer arithmetic:<\/strong> Go has none, so <code>p++<\/code> on a pointer does not compile.<\/li>\n\n\n<li><strong>Printing addresses in tests:<\/strong> addresses change every run, so never compare them as text.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Best Practices for Go Pointer Programs<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Use pointer receivers<\/strong> for methods that change a struct.<\/li>\n\n\n<li><strong>Return pointers from constructors<\/strong> like <code>NewOrder<\/code>, since callers usually change the value.<\/li>\n\n\n<li><strong>Check for nil<\/strong> at the start of functions that accept pointers.<\/li>\n\n\n<li>**Use *T for optional fields** in JSON and database structs.<\/li>\n\n\n<li><strong>Keep pointers simple<\/strong>, and avoid pointer-to-pointer unless you truly need it.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Go Pointer Programs Interview Questions and Answers<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Interviewers use short Go pointer programs to test how pointers behave. Each program is complete, with real output from Go 1.27.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Basic Questions on Go Pointer Programs<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q1. What happens when you dereference a nil pointer?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    var pointer *int      \/\/ nil, points to nothing\n    fmt.Println(*pointer) \/\/ reading through nil crashes\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>panic: runtime error: invalid memory address or nil pointer dereference<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The program crashes with a panic. This is the most common runtime error in Go, so always check pointers that might be nil.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q2. Does Go support pointer arithmetic?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">No. You cannot add to or subtract from a pointer like in C. This removes a whole class of memory bugs. The <code>unsafe<\/code> package can do it, but normal code should never need it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q3. What is the difference between new and make?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>new(T)<\/code> returns a <code>*T<\/code> pointing to a zero value, and works for any type. <code>make<\/code> only creates slices, maps and channels, and returns the ready value itself, not a pointer.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    slicePointer := new([]int)  \/\/ a pointer to a nil slice\n    numbers := make([]int, 2)   \/\/ a ready slice with 2 zeros\n    fmt.Println(*slicePointer == nil, len(numbers))\n\n    ages := make(map[string]int) \/\/ a ready, empty map\n    ages[\"a\"] = 1\n    fmt.Println(ages)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>true 2\nmap[a:1]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>new([]int)<\/code> gives a pointer to an empty slice that is still nil. <code>make<\/code> gives you a slice or map you can use straight away.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q4. What does this program print?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    first, second := 1, 1\n    firstPointer := &amp;first\n    secondPointer := &amp;second\n    fmt.Println(firstPointer == secondPointer, *firstPointer == *secondPointer)\n\n    samePointer := &amp;first\n    fmt.Println(firstPointer == samePointer)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>false true\ntrue<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Two pointers are equal only if they point to the same variable. <code>first<\/code> and <code>second<\/code> hold the same number but live at different addresses.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Function and Struct 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\/\/ reset sets its own copy of the pointer to nil.\nfunc reset(pointer *int) {\n    pointer = nil \/\/ changes only the local copy\n}\n\nfunc main() {\n    score := 5\n    pointer := &amp;score\n    reset(pointer)\n    fmt.Println(pointer != nil, *pointer)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>true 5<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The function gets a copy of the pointer. Setting that copy to nil does not change the caller&#8217;s <code>pointer<\/code>, which still points to <code>score<\/code>.<\/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\/\/ grow adds 4 to the slice that the pointer points to.\nfunc grow(numbers *[]int) {\n    *numbers = append(*numbers, 4) \/\/ store the new slice back at the address\n}\n\nfunc main() {\n    numbers := []int{1, 2, 3}\n    grow(&amp;numbers)\n    fmt.Println(numbers)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>[1 2 3 4]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Here the function must change the slice header itself, because <code>append<\/code> may return a new one. That is the one common case where a pointer to a slice makes sense. Returning the new slice is usually cleaner.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q7. 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 one number.\ntype Box struct{ Value int }\n\nfunc main() {\n    original := Box{1}\n    pointer := &amp;original   \/\/ points to original\n    duplicate := *pointer  \/\/ *pointer makes a copy of the value\n    pointer.Value = 2      \/\/ changes original\n    duplicate.Value = 3    \/\/ changes only the copy\n    fmt.Println(original.Value, pointer.Value, duplicate.Value)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>2 2 3<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>pointer<\/code> points to <code>original<\/code>, so changing <code>pointer.Value<\/code> changes <code>original<\/code>. But <code>duplicate := *pointer<\/code> makes a copy, so <code>duplicate<\/code> changes alone.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q8. Is it safe to return the address of a local variable?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Yes. Go&#8217;s escape analysis sees that the address leaves the function and puts the variable on the heap. The garbage collector frees it later when nothing points to it.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Output Questions<\/h3>\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\nfunc main() {\n    var pointers []*int\n    for i := range 3 { \/\/ i takes 0, 1, 2\n        pointers = append(pointers, &amp;i) \/\/ save the address of this pass's i\n    }\n    for _, pointer := range pointers {\n        fmt.Print(*pointer, \" \")\n    }\n    fmt.Println()\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>0 1 2<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Since Go 1.22, each loop pass has its own <code>i<\/code>, so every pointer points to a different variable. In older Go, this printed &#8220;3 3 3&#8221; or &#8220;2 2 2&#8221;.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q10. What does this program print?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    total := 10\n    pointer := &amp;total\n    *pointer += 5 \/\/ total is now 15\n    *pointer *= 2 \/\/ total is now 30\n    fmt.Println(total)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>30<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Both lines change <code>total<\/code> through the pointer. First it becomes 15, then 30.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q11. Why do Go APIs use pointers for optional JSON fields?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A plain field always has a value, so you cannot tell &#8220;the client sent 0&#8221; from &#8220;the client sent nothing&#8221;. A pointer field is nil when the value is missing, which keeps the two cases apart.<\/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\/\/ Car has a speed.\ntype Car struct{ Speed int }\n\n\/\/ Faster adds 10 to the speed and returns the same car pointer.\nfunc (car *Car) Faster() *Car {\n    car.Speed += 10\n    return car\n}\n\nfunc main() {\n    car := &amp;Car{}\n    car.Faster().Faster().Faster() \/\/ three calls in a chain\n    fmt.Println(car.Speed)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>30<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Each method returns the same pointer, so we can chain calls. This style is common in builder patterns.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Practice Task<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">This task mixes maps with Go pointer programs. Write a function <code>updateStock(stock map[string]*int, item string, change int)<\/code> that adds <code>change<\/code> to an item&#8217;s stock, and creates the item if it does not exist. Print the stock after three updates. Hint: when the item is missing, create a new int with <code>new(int)<\/code> and store its address.<\/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\/1\">A Tour of Go: Pointers<\/a><\/li>\n\n\n<li><a href=\"https:\/\/go.dev\/ref\/spec#Pointer_types\">Go spec: pointer types<\/a><\/li>\n\n\n<li><a href=\"https:\/\/go.dev\/doc\/faq#methods_on_values_or_pointers\">Go FAQ: when should I use a pointer to an interface or value<\/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 pointer programs use <code>&amp;<\/code> to take an address and <code>*<\/code> to read or change the value.<\/li>\n\n\n<li>Go passes copies, so pointers let a function change the caller&#8217;s data.<\/li>\n\n\n<li><code>new(T)<\/code> returns a pointer to a zero value, while <code>make<\/code> builds slices, maps and channels.<\/li>\n\n\n<li>A nil pointer dereference panics, so check for nil first.<\/li>\n\n\n<li>Go has no pointer arithmetic, and returning a pointer to a local variable is safe.<\/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 learn how different types can share behaviour. Read <a href=\"https:\/\/smitabharat.com\/blog\/go-interface-programs\/\">Go interface programs with output<\/a>, with shapes, Stringer and type switches. To revise structs first, see our <a href=\"https:\/\/smitabharat.com\/blog\/go-struct-programs\/\">Go struct programs<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Go pointer programs work with memory addresses, which let a function change a value that lives somewhere else. Pointers sound scary, but in Go&hellip;<\/p>\n","protected":false},"author":1,"featured_media":797,"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,32,104],"swapz_series":[],"class_list":["post-807","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-pointers","tag-go-programs"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Go Pointer Programs: Swap, nil Checks and new()<\/title>\n<meta name=\"description\" content=\"Go pointer programs with output: swap, change structs, new(), nil checks, optional values and nil receivers, plus pointer 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-pointer-programs\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Go Pointer Programs: Swap, nil Checks and new()\" \/>\n<meta property=\"og:description\" content=\"Go pointer programs with output: swap, change structs, new(), nil checks, optional values and nil receivers, plus pointer interview questions.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/smitabharat.com\/blog\/go-pointer-programs\/\" \/>\n<meta property=\"og:site_name\" content=\"Smita Bharat - Blog\" \/>\n<meta property=\"article:published_time\" content=\"2026-10-07T11:30:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-11-28T19:59:32+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-pointer-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=\"16 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-pointer-programs\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-pointer-programs\\\/\"},\"author\":{\"name\":\"Smita\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\"},\"headline\":\"Go Pointer Programs with Output: Swap, nil Checks and new()\",\"datePublished\":\"2026-10-07T11:30:00+00:00\",\"dateModified\":\"2026-11-28T19:59:32+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-pointer-programs\\\/\"},\"wordCount\":1846,\"publisher\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\"},\"image\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-pointer-programs\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-pointer-programs-featured.png\",\"keywords\":[\"Beginner\",\"Go Interview Questions\",\"Go Lang\",\"Go Pointers\",\"Go Programs\"],\"articleSection\":[\"Go Programs\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-pointer-programs\\\/\",\"url\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-pointer-programs\\\/\",\"name\":\"Go Pointer Programs: Swap, nil Checks and new()\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-pointer-programs\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-pointer-programs\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-pointer-programs-featured.png\",\"datePublished\":\"2026-10-07T11:30:00+00:00\",\"dateModified\":\"2026-11-28T19:59:32+00:00\",\"description\":\"Go pointer programs with output: swap, change structs, new(), nil checks, optional values and nil receivers, plus pointer interview questions.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-pointer-programs\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-pointer-programs\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-pointer-programs\\\/#primaryimage\",\"url\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-pointer-programs-featured.png\",\"contentUrl\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-pointer-programs-featured.png\",\"width\":2400,\"height\":1260,\"caption\":\"Go Pointer Programs featured image: swap, nil check and pointer output\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-pointer-programs\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Go Pointer Programs with Output: Swap, nil Checks and new()\"}]},{\"@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 Pointer Programs: Swap, nil Checks and new()","description":"Go pointer programs with output: swap, change structs, new(), nil checks, optional values and nil receivers, plus pointer 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-pointer-programs\/","og_locale":"en_US","og_type":"article","og_title":"Go Pointer Programs: Swap, nil Checks and new()","og_description":"Go pointer programs with output: swap, change structs, new(), nil checks, optional values and nil receivers, plus pointer interview questions.","og_url":"https:\/\/smitabharat.com\/blog\/go-pointer-programs\/","og_site_name":"Smita Bharat - Blog","article_published_time":"2026-10-07T11:30:00+00:00","article_modified_time":"2026-11-28T19:59:32+00:00","og_image":[{"width":2400,"height":1260,"url":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-pointer-programs-featured.png","type":"image\/png"}],"author":"Smita","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Smita","Est. reading time":"16 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/smitabharat.com\/blog\/go-pointer-programs\/#article","isPartOf":{"@id":"https:\/\/smitabharat.com\/blog\/go-pointer-programs\/"},"author":{"name":"Smita","@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339"},"headline":"Go Pointer Programs with Output: Swap, nil Checks and new()","datePublished":"2026-10-07T11:30:00+00:00","dateModified":"2026-11-28T19:59:32+00:00","mainEntityOfPage":{"@id":"https:\/\/smitabharat.com\/blog\/go-pointer-programs\/"},"wordCount":1846,"publisher":{"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339"},"image":{"@id":"https:\/\/smitabharat.com\/blog\/go-pointer-programs\/#primaryimage"},"thumbnailUrl":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-pointer-programs-featured.png","keywords":["Beginner","Go Interview Questions","Go Lang","Go Pointers","Go Programs"],"articleSection":["Go Programs"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/smitabharat.com\/blog\/go-pointer-programs\/","url":"https:\/\/smitabharat.com\/blog\/go-pointer-programs\/","name":"Go Pointer Programs: Swap, nil Checks and new()","isPartOf":{"@id":"https:\/\/smitabharat.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/smitabharat.com\/blog\/go-pointer-programs\/#primaryimage"},"image":{"@id":"https:\/\/smitabharat.com\/blog\/go-pointer-programs\/#primaryimage"},"thumbnailUrl":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-pointer-programs-featured.png","datePublished":"2026-10-07T11:30:00+00:00","dateModified":"2026-11-28T19:59:32+00:00","description":"Go pointer programs with output: swap, change structs, new(), nil checks, optional values and nil receivers, plus pointer interview questions.","breadcrumb":{"@id":"https:\/\/smitabharat.com\/blog\/go-pointer-programs\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/smitabharat.com\/blog\/go-pointer-programs\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/smitabharat.com\/blog\/go-pointer-programs\/#primaryimage","url":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-pointer-programs-featured.png","contentUrl":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-pointer-programs-featured.png","width":2400,"height":1260,"caption":"Go Pointer Programs featured image: swap, nil check and pointer output"},{"@type":"BreadcrumbList","@id":"https:\/\/smitabharat.com\/blog\/go-pointer-programs\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/smitabharat.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Go Pointer Programs with Output: Swap, nil Checks and new()"}]},{"@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\/807","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=807"}],"version-history":[{"count":1,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts\/807\/revisions"}],"predecessor-version":[{"id":888,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts\/807\/revisions\/888"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/media\/797"}],"wp:attachment":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/media?parent=807"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/categories?post=807"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/tags?post=807"},{"taxonomy":"swapz_series","embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/swapz_series?post=807"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}