Avatar ·

How to print the contents of a slice in Go?

Hello everyone! I just started learning Go and encountered a simple but unclear problem.

\n\n

I'm trying to print the contents of a slice, but instead of values I get something strange like [0x...]. Here's my code:

\n\n
package main\n\nimport "fmt"\n\nfunc main() {\n    arr := []int{10, 20, 30, 40, 50}\n    fmt.Println(arr)\n}\n
\n\n

When I run the program, it prints something like [10 20 30 40 50] — this works! But when I try to print a slice of strings or structs, everything breaks. Here's an example:

\n\n
package main\n\nimport "fmt"\n\ntype User struct {\n    Name string\n    Age  int\n}\n\nfunc main() {\n    users := []User{\n        {"Alice", 30},\n        {"Bob", 25},\n    }\n    fmt.Println(users)\n}\n