Learn Go Programming from Scratch - Part 49 - String Formatting - Part 01

preview_player
Показать описание
#chamiviews

🎥 Learn Go Programming from Scratch - Part 49 - String Formatting 📚

Welcome to Part 49 of our "Learn Go Programming from Scratch" series! In this video, we'll dive deep into the world of string formatting in Go. String formatting is a powerful feature that allows you to create well-formatted strings for various purposes, including displaying structured data, formatting numbers, and more.

### What You'll Learn:

- 🛠️ How to format and print structs using different verbs (`%v`, `%+v`, `%#v`)
- 📝 Displaying the type of variables with `%T`
- ✅ Formatting booleans with `%t`
- 🔢 Formatting integers in decimal, binary, and hexadecimal with `%d`, `%b`, `%x`
- 🔤 Formatting characters with `%c`
- 📊 Formatting floating-point numbers with `%f`, `%e`, and `%E`
- 🖋️ Formatting strings with `%s` and `%q`
- 📍 Formatting pointers with `%p`
- 📐 Using width and precision for numbers and strings

### Code Example:
```go
package main

import (
"fmt"
"os"
)

type point struct {
x, y int
}

func main() {

p := point{1, 2}
fmt.Printf("struct1: %v\n", p)

fmt.Printf("struct2: %+v\n", p)

fmt.Printf("struct3: %#v\n", p)

fmt.Printf("type: %T\n", p)

fmt.Printf("bool: %t\n", true)

fmt.Printf("int: %d\n", 123)

fmt.Printf("bin: %b\n", 14)

fmt.Printf("char: %c\n", 33)

fmt.Printf("hex: %x\n", 456)

fmt.Printf("float1: %f\n", 78.9)

fmt.Printf("float2: %e\n", 123400000.0)
fmt.Printf("float3: %E\n", 123400000.0)

fmt.Printf("str1: %s\n", "\"string\"")

fmt.Printf("str2: %q\n", "\"string\"")

fmt.Printf("str3: %x\n", "hex this")

fmt.Printf("pointer: %p\n", &p)

fmt.Printf("width1: |%6d|%6d|\n", 12, 345)

fmt.Printf("width2: |%6.2f|%6.2f|\n", 1.2, 3.45)

fmt.Printf("width3: |%-6.2f|%-6.2f|\n", 1.2, 3.45)

fmt.Printf("width4: |%6s|%6s|\n", "foo", "b")

fmt.Printf("width5: |%-6s|%-6s|\n", "foo", "b")
}
```

By the end of this video, you'll have a solid understanding of how to format strings effectively in Go. This is a crucial skill for any Go developer, whether you're formatting output for the console, generating logs, or preparing data for external APIs.

### Don't Forget to Subscribe! 👍
If you find this video helpful, please like, share, and subscribe to our channel for more content on Go programming. Stay tuned for the next part where we will explore more advanced concepts in Go.

🎉 Happy Coding! 🚀

#GoProgramming #GoLang #Coding #Programming #StringFormatting #LearnToCode #GoBasics #GoTutorial #SoftwareDevelopment #TechEducation #CodingTutorial #DevTips #GoDeveloper #CodeNewbie #GoStructs #GoTemplates #ProgrammingTips #LearnGo #TechTutorial #CodingFromScratch #DeveloperJourney
Рекомендации по теме