Fliptext em Go
O programa “fliptext” em Go exercita as funcões básicas da linguagem e é um bom pretexto para vê-la funcionando. O programa recebe argumentos pela linha-de-comando e devolve o texto invertido (horizontal e verticalmente). Na verdade, é uma simples transposição de caracteres para outros da tabela UTF-8 que sejam ou pareçam os primeiros invertidos.
Esse post contém caracteres que não são corretamente visualizados em todos os navegadores e sistemas operacionais! Funciona perfeitamente em Chrome/Mac.
Por exemplo:
˙sopᴉʇɹǝʌuᴉ soɹᴉǝɯᴉɹd so ɯɐɔǝɹɐd no ɯɐɾǝs ǝnb 8-ɟʇn ɐlǝqɐʇ ɐp soɹʇno ɐɹɐd sǝɹǝʇɔɐɹɐɔ ǝp oɐɔᴉsodsuɐɹʇ sǝldɯᴉs ɐɯn ǝ ‘ǝpɐpɹǝʌ ɐu ˙(ǝʇuǝɯlɐɔᴉʇɹǝʌ ǝ lɐʇuozᴉɹoɥ) opᴉʇɹǝʌuᴉ oʇxǝʇ o ǝʌloʌǝp ǝ opuɐɯoɔ-ǝp-ɐɥuᴉl ɐlǝd soʇuǝɯnᵷɹɐ ǝqǝɔǝɹ ɐɯɐɹᵷoɹd o ˙opuɐuoᴉɔunɟ ɐl-ǝʌ ɐɹɐd oʇxǝʇǝɹd ɯoq ɯn ǝ ǝ ɯǝᵷɐnᵷuᴉl ɐp sɐɔᴉsɐq sǝõɔunɟ sɐ ɐʇᴉɔɹǝxǝ oᵷ ɯǝ ”ʇxǝʇdᴉlɟ“ ɐɯɐɹᵷoɹd o
Veja o código fonte:
/* Main stuff */ package main import ("os"; "flag"; "strings") func init() { flag.Parse(); loadList() } func main() { os.Stdout.WriteString(FlipText(GetStr()) + "\n") } func GetStr() string { return strings.ToLower(strings.Join(flag.Args(), " ")) } /* Flip */ func FlipText(s string) string { // Função que faz a inversão do texto c := []int(s) // string para array de inteiros j := len(c) if h := j/2; j % 2 != 0 { c[h] = flipChar(c[h]) } // Se impar, inverte o central j-- for i := 0; i < j; i, j = i+1, j-1 { // Inverte os carac. das extrem. p/ centro c[i], c[j] = flipChar(c[j]), flipChar(c[i]) } return string(c) } func flipChar(i int) int { // Função que inverte um caracter if List[i] != 0 { return List[i] } return i } /* List */ var List = make(map[int]int) // Lista assoc. de carac. func loadList() { // Carrega a lista de carac. for _, v := range strings.Split(strList1, "\n", 0) { // Unívocos if v != "" { arr := []int(v) List[arr[0]] = arr[2] } } for _, v := range strings.Split(strList2, "\n", 0) { // Biunívocos if v != "" { arr := []int(v) List[arr[0]], List[arr[2]] = arr[2], arr[0] } } } /* Pre-List */ // Unívocos var strList1 = ` ã ɐ á ɐ à ɐ â ɐ é ǝ ê ǝ í ᴉ î ᴉ ó o ô o ú n ç ɔ 4 h 5 s ` // Biunívocos var strList2 = ` a ɐ b q c ɔ d p e ǝ f ɟ g ᵷ h ɥ i ᴉ j ɾ k ʞ l l m ɯ n u p d q b r ɹ t ʇ u n v ʌ w ʍ y ʎ ( ) [ ] { } < > ! ¡ ? ¿ . ˙ , ' _ ‾ 1 ⇂ 2 ᄅ 3 ᄐ 6 9 7 ㄥ `
Compile:
# Em vez de 6... ajuste para sua arquitetura ( 5, 6 ou 8 )
6g fliptext.go
6l -o fliptext fliptext.6
E use pelo terminal assim:
./fliptext <um texto qualquer>
Por exemplo:
./fliptext 'gostou?'
¿noʇsoᵷ





