funcEncode(s string, alphabet map[string]string, letterSep string, wordSep string)string { res := "" for _, part := range s { p := string(part) if p == " " { if wordSep != "" { res += wordSep + letterSep } } elseif morseITU[p] != "" { res += morseITU[p] + letterSep } } return strings.TrimSpace(res) }
funcDecode(s string, alphabet map[string]string, letterSep string, wordSep string)(string, error) { res := "" for _, part := range strings.Split(s, letterSep) { found := false for key, val := range alphabet { if val == part { res += key found = true break } } if part == wordSep { res += " " found = true } if found == false { return res, fmt.Errorf("unknown character " + part) } } return res, nil }