DEV Community

Cover image for How to Work with Arrays and Use Pointers with Arrays in Go
Ganesh Kumar
Ganesh Kumar

Posted on

How to Work with Arrays and Use Pointers with Arrays in Go

#go

Hello, I'm Ganesh Kumar. I'm working on git-lrc: a Git hook for Checking AI generated code.
AI agents write code fast. They also silently remove logic, change behavior, and introduce bugs -- without telling you. You often find out in production.
git-lrc fixes this. It hooks into git commit and reviews every diff before it lands. 60-second setup. Completely free.

Here is the corrected version of your blog post with improved grammar and flow, while maintaining your personal style:

In my previous post, we learned about struct data type.
Now let's learn about array data type.

Array Data Type

Array is a collection of elements of the same type.

Declaring Array

Array is declared using var keyword.

var array_name [size]type
Enter fullscreen mode Exit fullscreen mode

size is the number of elements in the array.

type is the type of the elements in the array.

Index Assignment

let's declare an array of strings with size 5 and assign values to it.

package main

import "fmt"

func main() {
    var s [5]string
    s[0] = "Hello"
    s[1] = "World"
    fmt.Println(s)
    fmt.Println(s[0])
    fmt.Println(s[1])
}
Enter fullscreen mode Exit fullscreen mode

Here, we assigned values to the first two elements of the array using index.

s[0] is the first element of the array with Hello value.

s[1] is the second element of the array with World value.

Output:

gk@jarvis:~/exp/code/rd/go-exmaple$ go run main.go
[Hello World   ]
Hello
World
Enter fullscreen mode Exit fullscreen mode

Printing the array using fmt.Println prints the array in the format {element1 element2 element3 ...}.

Printing the index element will be in the format array_name[index].

Short Variable Declaration

let's declare an array of strings with size 5 and assign values to it using short variable declaration.

import "fmt"

func main() {
    s := [5]string{"Hello", "World"}
    fmt.Println(s)
    fmt.Println(s[0])
    fmt.Println(s[1])
}

Enter fullscreen mode Exit fullscreen mode

Output:

gk@jarvis:~/exp/code/rd/go-exmaple$ go run main.go
[Hello World   ]
Hello
World
Enter fullscreen mode Exit fullscreen mode

As we didn't change any print statement, we got same output as previous example.

Update Array

let's update the array elements.

package main

import "fmt"

func main() {
    var s [5]string
    s[0] = "Hello"
    s[1] = "World"
    fmt.Println(s)
    fmt.Println(s[0])
    fmt.Println(s[1])
    s[0] = "Hi"
    fmt.Println(s)
    fmt.Println(s[0])
}
Enter fullscreen mode Exit fullscreen mode

Output:

gk@jarvis:~/exp/code/rd/go-exmaple$ go run main.go
[Hello World   ]
Hello
World
[Hi World   ]
Hi
Enter fullscreen mode Exit fullscreen mode

We can see Hello is replaced with Hi.

Using Pointer With Array

let's declare an array of strings with size 5 and assign values to it using pointer.

package main

import "fmt"

func main() {
    var s [5]string
    p := &s

    p[0] = "Hello"
    p[1] = "World"

    fmt.Println(p[0])
    fmt.Println(p[1])
    fmt.Println(s)
}
Enter fullscreen mode Exit fullscreen mode

Output:

gk@jarvis:~/exp/code/rd/go-exmaple$ go run main.go
Hello
World
[Hello World   ]
Enter fullscreen mode Exit fullscreen mode

Conclusion

In this we learned about array data type and we also used pointer with array.

git-lrc

πŸ‘‰ Check out: git-lrc
Any feedback or contributors are welcome! It’s online, open-source, and ready for anyone to use.
⭐ Star it on GitHub:

GitHub logo HexmosTech / git-lrc

Free, Unlimited AI Code Reviews That Run on Commit

git-lrc logo

git-lrc

Free, Unlimited AI Code Reviews That Run on Commit


git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt

AI agents write code fast. They also silently remove logic, change behavior, and introduce bugs -- without telling you. You often find out in production.

git-lrc fixes this. It hooks into git commit and reviews every diff before it lands. 60-second setup. Completely free.

See It In Action

See git-lrc catch serious security issues such as leaked credentials, expensive cloud operations, and sensitive material in log statements

git-lrc-intro-60s.mp4

Why

  • πŸ€– AI agents silently break things. Code removed. Logic changed. Edge cases gone. You won't notice until production.
  • πŸ” Catch it before it ships. AI-powered inline comments show you exactly what changed and what looks wrong.
  • πŸ” Build a habit, ship better code. Regular review β†’ fewer bugs β†’ more robust code β†’ better results in your team.
  • πŸ”— Why git? Git is universal. Every editor, every IDE, every AI…




Top comments (0)