Created
January 27, 2024 16:32
-
-
Save martin-mok/10080ce0fef349caa9690e1f19801b04 to your computer and use it in GitHub Desktop.
A Tour of Go Exercise: Stringers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Other method to convert byte number to string:
s := strconv.Itoa(int(byte))
https://golang.cafe/blog/golang-int-to-string-conversion-example.html