[Go]Replace characters in string.

The string is a physical address, then this is split into an array, which then captures the last element of the array. This last element is the actual mac address, then replace all “-” with “:” with the strings package.
For converting cisco mac address format, first remove all “-” then slice the strings up, then join the slice with dot.

package main

import (
	"fmt"
	"strings"
)

func main() {
	phyAddr := "Physical Address. . . . . . . . . : 38-00-25-9F-58-30"
	sSlice := strings.Split(phyAddr, " ")
	macAddr := sSlice[len(sSlice) -1]
	ciscoAddr := convCicso(macAddr)
	fmt.Println("Windows mac address format:",macAddr)
	fmt.Println("Linux mac address format:", strings.Replace(macAddr, "-", ":", -1))
	fmt.Println("Cisco mac address format:", ciscoAddr)
}

func convCicso(m string) string {
	c := strings.Replace(m, "-", "", -1)
	c = c[:4] + "." + c[4:8] + "." + c[8:12]
	return strings.ToLower(c)
}

The result:
go1

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s