Skip to content

Instantly share code, notes, and snippets.

@danimal141
Last active June 15, 2026 20:47
Show Gist options
  • Select an option

  • Save danimal141/040a3d03598a6a51553a7a839f9d2d5a to your computer and use it in GitHub Desktop.

Select an option

Save danimal141/040a3d03598a6a51553a7a839f9d2d5a to your computer and use it in GitHub Desktop.
A Tour of Go Exercise: Stringers
package main
import (
"fmt"
"strings"
"strconv"
)
type IPAddr [4]byte
func (ip IPAddr) String() string {
s := make([]string, len(ip))
for i, val := range ip {
s[i] = strconv.Itoa(int(val))
}
return strings.Join(s, ".")
}
func main() {
hosts := map[string]IPAddr{
"loopback": {127, 0, 0, 1},
"googleDNS": {8, 8, 8, 8},
}
for name, ip := range hosts {
fmt.Printf("%v: %v\n", name, ip)
}
}
@tejas-kr

Copy link
Copy Markdown

Mine lol

package main

import "fmt"

type IPAddr [4]byte

// TODO: Add a "String() string" method to IPAddr.
func (ip_add IPAddr) String() string {
	return fmt.Sprintf("%d.%d.%d.%d", ip_add[0],ip_add[1],ip_add[2],ip_add[3])
}

func main() {
	hosts := map[string]IPAddr{
		"loopback":  {127, 0, 0, 1},
		"googleDNS": {8, 8, 8, 8},
	}
	for name, ip := range hosts {
		fmt.Printf("%v: %v\n", name, ip)
	}
}

@warhammer327

warhammer327 commented Oct 10, 2024

Copy link
Copy Markdown
package main

import "fmt"

type IPAddr [4]byte

// TODO: Add a "String() string" method to IPAddr.

func (ip IPAddr) String() string {
	return fmt.Sprintf("%v.%v.%v.%v\n", ip[0],ip[1], ip[2], ip[3])
}

func main() {
	hosts := map[string]IPAddr{
		"loopback":  {127, 0, 0, 1},
		"googleDNS": {8, 8, 8, 8},
	}
	for name, ip := range hosts {
		fmt.Printf("%v: %v\n", name, ip)
	}
}

@priyanshoon

Copy link
Copy Markdown
package main

import "fmt"

type IPAddr [4]byte

func (ip IPAddr) String() string {
	return fmt.Sprintf("%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3])
}

func main() {
	hosts := map[string]IPAddr{
		"loopback":  {127, 0, 0, 1},
		"googleDNS": {8, 8, 8, 8},
	}
	for _, ip := range hosts {
		fmt.Println(ip.String())
	}
}

Nice answer, I couldn't think of it

@priyanshoon

Copy link
Copy Markdown
package main

import "fmt"

type IPAddr [4]byte

// TODO: Add a "String() string" method to IPAddr.
func (ip IPAddr) String() string {
	var ipFinal string = ""
	for i := range ip {
		if i < 3 {
			ipFinal = ipFinal + fmt.Sprintf("%d", ip[i]) + "."
		} else {
			ipFinal = ipFinal + fmt.Sprintf("%d", ip[i])
		}
	}
	return ipFinal
}

func main() {
	hosts := map[string]IPAddr{
		"loopback":  {127, 0, 0, 1},
		"googleDNS": {8, 8, 8, 8},
	}
	for name, ip := range hosts {
		fmt.Printf("%v: %v\n", name, ip)
	}
}

@filipcvejic

Copy link
Copy Markdown
package main

import (
	"fmt"
	"strings"
	"strconv"
)

type IPAddr [4]byte

// TODO: Add a "String() string" method to IPAddr.
func (ip IPAddr) String() string {
	s := make([]string, len(ip))
	for i, val := range ip {
		s[i] = strconv.Itoa(int(val))
	}
	return strings.Join(s, ".")
}

func main() {
	hosts := map[string]IPAddr{
		"loopback":  {127, 0, 0, 1},
		"googleDNS": {8, 8, 8, 8},
	}
	for name, ip := range hosts {
		fmt.Printf("%v: %v\n", name, ip)
	}
}

@duj4

duj4 commented Jul 5, 2025

Copy link
Copy Markdown

I'm using append instead of concat string

package main

import (
	"fmt"
	"strings"
	"strconv"
)

type IPAddr [4]byte

func (ip IPAddr) String() string {
	s := make([]string, len(ip))
	
	for _, v := range ip {
		s = append(s, strconv.Itoa(int(v)))
	} 
	
	return strings.Join(s, ".")
}

func main() {
	hosts := map[string]IPAddr{
		"loopback":  {127, 0, 0, 1},
		"googleDNS": {8, 8, 8, 8},
	}
	for name, ip := range hosts {
		fmt.Printf("%v: %v\n", name, ip)
	}
}

I think it'd be better to declare the slice s like make([]string, 0, len(ip)). Otherwise, the output would be like "....127.0.0.1" as the append will add the digit from index 4.

@batic420

Copy link
Copy Markdown

I've tried to keep it simple and used the strings.Builder type to create the desired result

package main

import (
	"fmt"
	"strings"
)

type IPAddr [4]byte

func (ip IPAddr) String() string {
	var b strings.Builder
	for _, v := range ip {
		fmt.Fprintf(&b, "%d.", v)
	}
	return strings.Trim(b.String(), ".")
}

func main() {
	hosts := map[string]IPAddr{
		"loopback":   {127, 0, 0, 1},
		"googleDNS":  {8, 8, 8, 8},
	}
	for name, ip := range hosts {
		fmt.Printf("%v: %v\n", name, ip)
	}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment