|
| 1 | +Tutorial |
| 2 | +-------- |
| 3 | +The builtin `fmt` package provides many useful functions which are used to print to console or format to a string. Let's take a look at few of the most used functions. |
| 4 | + |
| 5 | +## fmt.Println() |
| 6 | + |
| 7 | +`fmt.Println()` is the basic print function golang has to offer. It prints the values given to it seperated by commas and adds a newline at the end. |
| 8 | + |
| 9 | + // fmt.Println() usage |
| 10 | + fmt.Println("Hello") // prints "Hello" |
| 11 | + |
| 12 | + // multiple arguments of different types can be given seperated by commas |
| 13 | + fmt.Println("Hello", 22, 98.3, true) // prints "Hello 22 98.3 true" |
| 14 | + |
| 15 | + // you can also pass variables as arguments |
| 16 | + a := "Hello" |
| 17 | + b := "World" |
| 18 | + |
| 19 | + fmt.Println(a, b) // prints "Hello World" |
| 20 | + |
| 21 | +## fmt.Printf() |
| 22 | + |
| 23 | +`fmt.Printf()` is similar to `printf()` in C language. We give a string with keywords like `%d`, `%v` and give the other arguments which get formatted to these keywords. |
| 24 | + |
| 25 | + // fmt.Printf() usage |
| 26 | + fmt.Printf("%s is %d years old.", "Jon Snow", 30) // prints "Jon Snow is 30 years old." |
| 27 | + |
| 28 | + // using %v for printing everything |
| 29 | + fmt.Printf("%v is %v years old.", "Jon Snow", 30) // prints "Jon Snow is 30 years old." |
| 30 | + |
| 31 | + // let's try printing type of any variable |
| 32 | + name := "Jon Snow" |
| 33 | + fmt.Printf("%T is the type of %v", name, name) // prints "string is the type of Jon Snow" |
| 34 | + |
| 35 | +Common keywords that are used and what variables are formatted by them |
| 36 | + |
| 37 | +`%d` - integers |
| 38 | +`%s` - strings |
| 39 | +`%f` - floating point number |
| 40 | +`%t` - boolean |
| 41 | + |
| 42 | + |
| 43 | +`%T` - prints the type of the variable given |
| 44 | +`%v` - prints any object of any type |
| 45 | + |
| 46 | + |
| 47 | +## fmt.Sprintf() |
| 48 | + |
| 49 | +`fmt.Sprintf()` works just like `fmt.Printf()` but instead of printing to the console it returns the formatted string. It's highly useful for creating formatted strings from variables in golang. |
| 50 | + |
| 51 | +Let's see the function in action. |
| 52 | + |
| 53 | + // the function returns a formatted string. |
| 54 | + s := fmt.Sprintf("%s is the son of %s", "Harry", "Lily") // returns a string "Harry is the son of Lily" |
| 55 | + |
| 56 | + // let's print the string s |
| 57 | + fmt.Println(s) // prints "Harry is the son of Lily" |
| 58 | + |
| 59 | +The same keywords that we used for formatting in `fmt.Printf()` apply here too. |
| 60 | + |
| 61 | +Exercise |
| 62 | +-------- |
| 63 | +Given two variables, a person's name and his favourite movie, format it and print it to console in the given format. For example the person is `Nolan` and his favourite movie is `Tenet`, your output string should be `Tenet is Nolan's favourite movie`. Use `fmt.Printf()` |
| 64 | + |
| 65 | +Tutorial Code |
| 66 | +------------- |
| 67 | +package main |
| 68 | + |
| 69 | +import "fmt" |
| 70 | + |
| 71 | +func main() { |
| 72 | + name := "Leonardo" |
| 73 | + movie := "Lord of the Rings" |
| 74 | + |
| 75 | + // your code here |
| 76 | +} |
| 77 | + |
| 78 | +Expected Output |
| 79 | +--------------- |
| 80 | +Lord of the Rings is Leonardo's favourite movie |
| 81 | + |
| 82 | +Solution |
| 83 | +-------- |
| 84 | +package main |
| 85 | + |
| 86 | +import "fmt" |
| 87 | + |
| 88 | +func main() { |
| 89 | + name := "Leonardo" |
| 90 | + movie := "Lord of the Rings" |
| 91 | + |
| 92 | + fmt.Printf("%s is %s's favourite movie", movie, name) |
| 93 | +} |
0 commit comments