7
\$\begingroup\$

I am a java developer and I am trying to learn Go. I tried to make a simple code that take numbers from a .txt file and make print them in a prettier format. That is:

0613228745     -> 06 13 22 87 45
06.13.22.87.45 -> 06 13 22 87 45
06-13-22-87-45 -> 06 13 22 87 45
06 13 228 745  -> 06 13 22 87 45
0 613 228 745  -> 06 13 22 87 45

Can you review, give me some advices about my code ? Here it is:

package main

import (
    "bufio"
    "fmt"
    "os"
    "unicode"
)

func check(e error) {
    if e != nil {
        panic(e)
    }
}

func main() {

    file, err := os.Open("./phonenumbers.txt")
    check(err)

    scanner := bufio.NewScanner(file)
    for scanner.Scan() {
        basicPhoneNumberArray, prettyPhoneNumber := make([]rune, 0), ""
        // get only digit numbers
        for _, r := range []rune(scanner.Text()) {
            if unicode.IsDigit(r) {
                basicPhoneNumberArray = append(basicPhoneNumberArray, r)
            }
        }
        // put spaces every two digits
        for i, r := range basicPhoneNumberArray {
            prettyPhoneNumber += string(r)
            if i%2 != 0 && i != len(basicPhoneNumberArray)-1 { // no space at the end
                prettyPhoneNumber += " "
            }
        }
        fmt.Println(prettyPhoneNumber)
    }
}

Also I am very fond of functional programming, I think there should be a way to use some filter and map here but after few searches I didn't find a way. If you could give me some advices about that too that would be great :D.

\$\endgroup\$
  • \$\begingroup\$ Can you be specific about what you mean by "prettier"? You want them all in pairs, French style? What if there are an odd number of digits? \$\endgroup\$ – 200_success Feb 2 at 18:05
  • 1
    \$\begingroup\$ Yeah the goal is to turn them in the French style as you said: 2 digits, 1 space, etc. But I do not seek review about covering all possibilities or having the good result, it's more about my go code, is it efficient, can it be improved, am I using the right functions for the same result ? etc. \$\endgroup\$ – Franck Boucher Feb 2 at 19:55
  • \$\begingroup\$ Cross posted to Reddit: Asking for review: Prettify phone numbers. \$\endgroup\$ – peterSO Feb 3 at 22:12
1
\$\begingroup\$

Your variable names are way too long. If you extract the pretty-printing into a function called FormatFrenchPhone, the variables can be much shorter:

  • basic — doesn't convey any meaning, just remove it
  • phone — that's clear from the context, just remove it
  • number – this part provides useful information
  • array — that's clear from the variable declaration already

This leaves you with the name number, which i don't like because it's ambiguous in English. It can either mean the mathematical concept or the identification of something. (An employee number does not need to be numeric at all, for example.) I would instead name that variable digits. The plural s already says it's a slice or an array or a collection.

The other variable should be called pretty. It should be declared as var pretty strings.Builder if you want your code to look good, and pretty := make([]rune, len(digits) + (len(digits)+1)/2) if you want to avoid unnecessary memory allocation at every cost.

Since you come from a Java background, using strings.Builder should feel familiar.

\$\endgroup\$
1
\$\begingroup\$
  1. If you don't want to handle an error, then ignore it by using the _ symbol.
    I.E. replace:
    file, err := os.Open("./phonenumbers.txt")
    check(err)
    
    with:
    file, _ := os.Open("./phonenumbers.txt")
    
  2. Move the entire scanning logic into a separate function (you did write you are fond of functional programming 😉).
    func PrettifyNumber(original string) string
  3. You could use the strings.Fields to filter out any non numeric character.

    func IsNotADigit(r rune) bool {
        return !unicode.IsDigit(r)
    }
    
    func PrettifyNumber(original string) string {
        return strings.Join(strings.FieldsFunc(original, IsNotADigit), " ")
    }
    
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.