# GO(Golang) Variables ###### tags: `golang` [TOC] # Naming ```go= var speed int //Inside same file var Speed2 int //Available for whole package ``` **Note:** 1. Usually start with underscore character(_) because if we start with uppercase letter, it will be exported and be available for any package. 2. Name **can NOT start with** letter not have punctuation inside/start/end with it. 3. **MUST** always start with **letter** or **underscore**. 4. **MUST** state the value type, as GO is a strong programming language. ![](https://i.imgur.com/efaStse.png =200x100) # Declaration ```go= var speed int var dir, file string ``` ## Common VS short Declaration ```go= //DON'T! // width = 50 //assign 50 to width // color := "red" //new a variable called color and give red value width, color := 50, "blue" ``` ## Short Declaration ```go= func ShortDeclaration() { _, file := path.Split("css/main.css") fmt.Println("file: ", file) } ``` ## When to use short declaration? When we know # Value Conversion ```go= type(value) float64(speed) ``` # Go Common Naming Abbreviations ![](https://i.imgur.com/W1re7Cj.png =400x300) # String Variables ## String Types ### String literal You will use "" to state the string. You may need to use \ to let some characters to be shown. ### Raw String literal You will use `` to state the string. It will print as it is shown. ### Example ```go= sTest := "<html>\n\t</html>" fmt.Println(sTest) sTest2 := `<html> </html>` fmt.Println(sTest2) //Print Result // <html> // </html> // <html> // </html> ``` ## String Length ### len() is not appropiate for length count len(string) is used to calculate bytes in string. For non english characters, it returns wrong lenght. That is why we do not use it to count. ```go= sTest3 := "你好" fmt.Println(len(sTest3)) //This returns 6, even though it has only 2 characters. ``` ### utf8 counting method By importing **unicode/utf8**, we will obtain the characters(code points/single rooms) inside the string. ```go= sTest3 := "你好" fmt.Println(utf8.RuneCountInString(sTest3)) //This one returns 2 in lenght ``` ## String functions String functions can be found when using **strings**.function name. ```go= //If run file by using "go run main.go hii" msg := os.Args[1] l := len(msg) msg = strings.ToUpper(msg) exclamationMark := strings.Repeat("!", l) fmt.Println(exclamationMark + msg + exclamationMark) //Prints: !!!HII!!! ```