Golang String Formatting

GoGoBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

This challenge aims to test your knowledge of string formatting in Golang. You will be required to format different types of data such as integers, floats, strings, and structs using various formatting verbs.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL go(("`Go`")) -.-> go/DataTypesandStructuresGroup(["`Data Types and Structures`"]) go/DataTypesandStructuresGroup -.-> go/strings("`Strings`") subgraph Lab Skills go/strings -.-> lab-15431{{"`Golang String Formatting`"}} end

String Formatting

You are required to format different types of data using various formatting verbs in Golang.

Requirements

  • You must use the fmt package to format the data.
  • You must use the correct formatting verb for each data type.
  • You must be able to format integers, floats, strings, and structs.
  • You must be able to control the width and precision of the output.
  • You must be able to left-justify or right-justify the output.

Example

$ go run string-formatting.go
struct1: {1 2}
struct2: {x:1 y:2}
struct3: main.point{x:1, y:2}
type: main.point
bool: true
int: 123
bin: 1110
char: !
hex: 1c8
float1: 78.900000
float2: 1.234000e+08
float3: 1.234000E+08
str1: "string"
str2: "\"string\""
str3: 6865782074686973
pointer: 0xc0000ba000
width1: | 12 | 345 \
  | width2: | 1.20 | 3.45 \
  | width3: | 1.20 | 3.45 \
  | width4: | foo | b \
  | width5: | foo | b \
  | sprintf: a string
io: an error

Summary

This challenge tested your knowledge of string formatting in Golang. You learned how to format different types of data using various formatting verbs and how to control the width and precision of the output. You also learned how to left-justify or right-justify the output.

Other Go Tutorials you may like