NASutils Knowledge BaseUnofficialPolski

Go · lesson 5 of 30 · First stepsWork with text

The package strings cuts and searches. range gives the characters of a text, len counts its bytes.

Programming on the NAS › Go in 30 lessons

05-text.go Download 05-text.go

package main

import (
	"fmt"
	"path/filepath"
	"strings"
)

func main() {
	file := "Holiday-2027.JPG"
	fmt.Println("length:", len(file))
	fmt.Println("first 7:", file[:7])
	fmt.Println("the dash is at", strings.Index(file, "-"))
	fmt.Println("small letters:", strings.ToLower(file))
	fmt.Println("ending:", filepath.Ext(file))
	fmt.Println("new ending:", strings.TrimSuffix(file, ".JPG")+".txt")
	fmt.Println("parts:", strings.Split("3,Example HDD B,12000", ","))

	// len counts bytes; a range over the text gives whole letters
	word := "żółw"
	letters := 0
	for range word {
		letters++
	}
	fmt.Println(len(word), "bytes,", letters, "letters")
}

Terminal

$ ./05-text
length: 16
first 7: Holiday
the dash is at 7
small letters: holiday-2027.jpg
ending: .JPG
new ending: Holiday-2027.txt
parts: [3 Example HDD B 12000]
7 bytes, 4 letters

Exercise

Print a text in reverse, also with Polish letters.

TurboShell does not contain Go. These programs are made in Bucket & Code with its Go runtime; the finished file runs on the NAS without Go.