Skip to content

Instantly share code, notes, and snippets.

@martin-mok
Created January 27, 2024 16:32
Show Gist options
  • Save martin-mok/10080ce0fef349caa9690e1f19801b04 to your computer and use it in GitHub Desktop.
Save martin-mok/10080ce0fef349caa9690e1f19801b04 to your computer and use it in GitHub Desktop.
A Tour of Go Exercise: Stringers
package main
import (
"strings"
"fmt"
)
type IPAddr [4]byte
func (ips IPAddr) String() string {
var ips_string [4]string
for i, ip := range ips {
ips_string[i] = fmt.Sprint(ip)
}
return strings.Join(ips_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)
}
}
@martin-mok
Copy link
Author

Other method to convert byte number to string:
s := strconv.Itoa(int(byte))
https://golang.cafe/blog/golang-int-to-string-conversion-example.html

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