How to print the slice of pointers to get the values instead of their address without iteration at go?

How to print the slice of pointers to get the values instead of their address without iteration at go?

This kind output required for debugging purpose. To get actual value of slice of pointers, every time a iteration is getting required.

This kind output required for debugging purpose. To get actual value of slice of pointers, every time a iteration is getting required.

Is there any way, we can directly have the value instead of the address of each item present at slice using simple fmt.printf()?

Here is a code snippet : https://play.golang.org/p/bQ5vWTlKZmV

package main

import ( "fmt" )

type user struct { userID int name string email string }

func main() { var users []*user addUsers(users) }

func addUsers(users []*user) { users = append(users, &user{userID: 1, name: "cooluser1", email: "[email protected]"}) users = append(users, &user{userID: 2, name: "cooluser2", email: "[email protected]"}) printUsers(users) printEachUser(users)

}

func printUsers(users []*user) { fmt.Printf("users at slice %v \n", users) }

func printEachUser(users []*user) { for index, u := range users { fmt.Printf("user at user[%d] is : %v \n", index, *u) } }

At above code, if I am printing the slice directly by fmt.printf , I get only the address of the values instead of actual value itself.

output : users at slice [0x442260 0x442280]

To read to the values always, i have to call func like printEachUser to iterate the slice and get the appropriate value .

output: user at user[0] is : {1 cooluser1 [email protected]} user at user[1] is : {2 cooluser2 [email protected]}

Is there any way, we can read the values inside the slice of pointers using fmt.printf and get value directly like below ?

users at slice [&{1 cooluser1 [email protected]} , &{2 cooluser2 [email protected]}]

go

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Tiny Go: Small Is Going Big

Ron Evans talks about TinyGo - a compiler for Go, written in Go itself, that uses LLVM to achieve very small, fast, and concurrent binaries that can also target devices where Go could never go before.

Secure HTTPS servers in Go

In this article, we are going to look at some of the basic APIs of the http package to create and initialize HTTPS servers in Go

An Introduction to Go for non-Go Developers

A brief introduction to Go for programmers who haven't used Go before: Why use it? The standard library. Why Go? Go has become very popular over the past few years, partly because of the simplicity of the language, but perhaps more importantly because of the excellent tooling.

Go Programming and Why should you learn Go Language?

Go provides you high performance like C/C++, super efficient concurrency handling like Java and fun to code like Python/Perl. Go with Golang and why go is the best programming language in the world and Why should you learn Go Language?