diff --git a/_posts/go_lang/2023-06-24-variables_and_data_types.md b/_posts/go_lang/2023-06-24-variables_and_data_types.md index 4988587..9c22ac5 100644 --- a/_posts/go_lang/2023-06-24-variables_and_data_types.md +++ b/_posts/go_lang/2023-06-24-variables_and_data_types.md @@ -77,6 +77,8 @@ This data type only has 2 values. ## String Any values written inside \`\` ( back quote ) or "" ( double quote) is refereed as a string. +A string is a set of characters it can be of a single character or can be of a multiple characters. + Examples: @@ -196,9 +198,14 @@ This specifier is used to display boolean values. ### %T This specifier is used to display the type of the data/variable. +### %c +This specifier is used to display the character value hold by a rune + ### %v This specifier is used to display any kind of data. For example it can do work of `%d`, `%s`, `%f` ,`%t` etc..... + + ### Examples of Format specifiers ``` package main diff --git a/_posts/go_lang/2024-08-23-loop_in_go_lang.md b/_posts/go_lang/2024-08-23-loop_in_go_lang.md index b2a901a..c1c31ef 100644 --- a/_posts/go_lang/2024-08-23-loop_in_go_lang.md +++ b/_posts/go_lang/2024-08-23-loop_in_go_lang.md @@ -83,4 +83,57 @@ for { `body` -> In this section of the loop we actually wite the code we want to run repetitively writing the body is also not mandatory I know it's useless but it is possible. -Doc was written in this livestream [[Learn Go Day 10]](https://www.youtube.com/watch?v=LX4DsvuJ7AA) +## Using For Loop With Range + +`range` keyword in go lang can pass a single value to the loop on every iteration till the end of the iterator. +for example if you range over `"vipin"` using for loop it will give the value `"v"` on the first run then `"i"` on the second run then `"p"` on the next one and so on. +Another use case is iterating using a integer. + +NOTE: Iterating over an integer can be done using range only after go version `1.22` you can check the same via command `go version`. + +Here is the syntax for the same + +``` +for := range { + + + +} +``` + +Example: + +``` +package main + +import ("fmt") + +func main(){ + + for element := range 10 { + + fmt.Println(element) + + } + +} +``` + +Output: +``` +0 +1 +2 +3 +4 +5 +6 +7 +8 +9 +``` + +It always print `1 value less` then the given value as it stats from `0`. + + +Doc was written in this livestream [[Learn Go Day 10]](https://www.youtube.com/watch?v=LX4DsvuJ7AA) and [[Learn Go Day 12]](https://www.youtube.com/watch?v=eE2Ouv6Bxvk) diff --git a/_posts/go_lang/2024-09-07-Advanced_data_type_in_golang.md b/_posts/go_lang/2024-09-07-Advanced_data_type_in_golang.md new file mode 100644 index 0000000..69dc605 --- /dev/null +++ b/_posts/go_lang/2024-09-07-Advanced_data_type_in_golang.md @@ -0,0 +1,153 @@ +--- +title: Advanced Data Types In Go Lang +categories: go +tags: [go] +--- + + +When we say advanced data type it means that we are learning about the another data types that exist in GO lang but required some extra knowledge to understand. + + +## String + +A string is a set of characters it can be of a single character or can be of a multiple characters. +In golang we write sting inside \`\` ( back quote ) or "" ( double quote) for example `string1` and `"s"` are two different strings in go lang `''` is not used with strings It's a separate data type that can hold a single character for more checkout this post [Variables and Data Types](https://lyfofvipin.github.io/variables_and_data_types). + +Here is the visual representation of a String: + +If-Else + +In a string we have index value of each character (we can call them rune also as they are a single character ) and we can access them using there index value for example `0` has `V` it can be written as `string_name[index_value]`. + +Here is an example for the same: + +``` +var1 := "test string" // True +var2 := 'test string' // Error +var3 := `test string` // True +``` + +### Output with String + +``` +package main + +import ("fmt") + +func main(){ + + name := "Vipin" + + fmt.Println(name) + + fmt.Println("Vipin Kumar") + + fmt.Printf("%s", "New String") +} +``` + +### Input with String + +``` +package main + +import "fmt" + +func main() { + + var name string + + fmt.Printf("Enter Your Name: ") + fmt.Scan(&name) + + fmt.Printf("Hello %s!\n", name) +} +``` + + + +There is a inbuilt function in Go to find the length of a sting we will study about functions and modules soon for now let's see how `len()` works. + +So `len()` function in GO lang gives you the length of the sting. + +For Example: +``` +package main + +import ("fmt") + +func main() { + + test_str := "vipin" + + fmt.Println( len(test_str) ) + + fmt.Println( len("Vipin Kumar") ) +} +``` + +Output: + +``` +go run Day12/len.go +5 +11 +``` + +There is 1 more method we will be using `DecodeRuneInString` it's from library `unicode/utf8` this method covert a `rune` into a `string`. + +Example: +``` + +``` + +### Looping Over A String + +Here are 2 most common ways to loop on a string one using the `range` and another is using normal for loop. + +### For Loop Without Range On Strings + +We can get the length of the string usine `len` function then we can use for loop to iterate from `0` to `length - 1` because string indexing starts from `0`. +let's say we have a string of length 5 so it's index values are `0,1,2,3,4`. + + +Example: +``` +package main + +import "fmt" + +func main() { + + name := "Vipin Kumar" + + length := len(name) + + for iterator := 0 ; iterator < length ; iterator += 1 { + + fmt.Printf( "Index Value %d has character %c.\n", iterator, name[iterator] ) + + } + +} +``` + +Output: + +``` +go run Day12/forwithoutrange.go +Index Value 0 has character V. +Index Value 1 has character i. +Index Value 2 has character p. +Index Value 3 has character i. +Index Value 4 has character n. +Index Value 5 has character . +Index Value 6 has character K. +Index Value 7 has character u. +Index Value 8 has character m. +Index Value 9 has character a. +Index Value 10 has character r. +``` + + +Doc was written in this livestream [[Learn Go Day 12]](https://www.youtube.com/watch?v=eE2Ouv6Bxvk) diff --git a/statics/img/string.png b/statics/img/string.png new file mode 100644 index 0000000..7893f57 Binary files /dev/null and b/statics/img/string.png differ