vendredi 14 mai 2021

The BEST way to read some strings and print the longest one in GO

I'm getting crazy. I'm trying to write a program which reads some strings (in my case 5) and prints the longest one. I have googled and searched here but i haven't fond anything...

package main

import "fmt"

func main() {
    var t1, t2, t3, t4, t5 string
    fmt.Scan(&t1, &t2, &t3, &t4, &t5)
    var l1, l2, l3, l4, l5 int
    var max int = -50000
    l1 = len(t1)
    l2 = len(t2)
    l3 = len(t3)
    l4 = len(t4)
    l5 = len(t5)
    var longest string = ""
    if l1 > max {
        l1 = max
    }
    if l2 > max {
        l2 = max
    }
    if l3 > max {
        l3 = max
    }
    if l4 > max {
        l4 = max
    }
    if l5 > max {
        l5 = max
    }

    fmt.Println(max)
}

Aucun commentaire:

Enregistrer un commentaire