Go format “placeholder” input and output similar to Python input


Goang’s FMT package implements formatted I/O functions, similar to C’s printf and scanf

Define sample types and variables

type Human struct {
Name string
var people = Human{Name:"zhangsan"}

Ordinary placeholder

Occupant Description Example Output
% v) Default format for corresponding values. Printf (“%v”, people) {zhangsan},
%+ v) When printing the structure, the field name Printf (“%+v”, people) {Name: zhangsan} is added.
%# v) The Gogrammatical representation of the corresponding value Printf (” V”, people) main. Human {Name:”zhangsan”}
% T) The Gogrammatical representation of the type of the corresponding value Printf (“% T”, people) main. Human
%% The literal percentile is not the placeholder for the value Printf (“%”)%

Boolean placeholder

Placeholder, Description, Example, Output
% t) true or false. Printf (“%t”, true) true

Integer placeholder

Placeholder, Description, Example, Output
% Printf (“%b”, 5)] 101
% c) Characters represented by the corresponding Unicode code code points in Printf (“% c”, 0x4E2D)]
% d) decimal representation * Printf (“% d”, 0x12) * 18
% o) Octal Representation * Printf (“% d”, 10)] 12
% The literal value of a character surrounded by single quotation marks is safely escaped from Go grammar to Printf (“% Q”, 0x4E2D)]’
% x) Hexadecimal representation, lowercase A-F Printf (“% x”, 13)] D
% X. Hexadecimal representation, capital A-F Printf (“% x”, 13)”D
% Unicode format: U + 1234, equivalent to “U+% 04X”, “Printf (“% U”, 0x4E2D)” U + 4E2D

Components of floating and complex numbers (real and imaginary parts)

Placeholder, Description, Example, Output
% b) A scientific method of counting exponents of two without decimal parts.
Consistent with the’b’conversion format of strconv.FormatFloat. For example – 123456p – 78
% e) Scientific counting, e.g. – 1234.456e+78 Printf (“% e”, 10.2) – 1.020000e+01
% E. Scientific counting, e.g. – 1234.456E+78 Printf (“%e”, 10.2)] 1.020000E+01
% f) There are decimal points without exponents, such as 123.456 Printf (“% f”, 10.2) 10.200000
% g) Select% e or% F as appropriate to produce a more compact (endless 0) output Printf (“% g”, 10.20) 10.2
% G) Select% E or% F as appropriate to produce a more compact (endless 0) output Printf (“% G”, 10.20 + 2i) (10.2 + 2i)

Strings and byte slices

Placeholder, Description, Example, Output
% s) Output string representation (string type or [] byte) Printf (“% s”, [] byte (“Go language”) Go language
% The string surrounded by double quotation marks is safely escaped by Go grammar from Printf (“% q”, “Go language”) to “Go language”
% x) Hexadecimal, lowercase letters, two characters per byte Printf (“% x”, “golang”) 676f6c616e67
% X) Hexadecimal, capital letter, two characters per byte Printf (“% X”, “golang”) 676F6C616E67

Placeholder, Description, Example, Output
% p) Hexadecimal representation, prefix 0x * Printf (“% p”, & people) > 0x4f57f0

Goang does not have’% U’bits. If the integer is of unsigned type, it will be printed unsigned by default.

The control format of width and precision is in Unicode code code points. The width is the minimum width of the area occupied by the value; the precision is the number of digits after the decimal point.

When the operand type is int, both width and precision can be represented by the character’*’.

For% g/% G, the accuracy is the total number of all the numbers, such as 123.45,%. 4G prints 123.5, (while% 6.2F prints 123.45).

% Default precision of E and% f is 6

For most numerical types, the width is the minimum number of characters to be output, and if necessary, spaces are filled in for the formatted form.

The maximum number of characters output is the precision of the string type, which will be truncated directly if necessary.

Read input from console

package main
import "fmt"
func main() {
 for i := 1; i <=5 ; i ++ {
  var score int
  Fmt. Printf ("Please enter a student's grade:")
  if (score==50) {
   Fmt. Printf ("You got a% V score, barely passed", score)
  }else {
 Fmt. Println ("for loop is over")


Above is the go format “placeholder” introduced by Xiaobian to you. Input and output, similar to python‘s input, hope to be helpful to you. If you have any questions, please leave me a message, Xiaobian will reply to you in time. Thank you very much for your support to developpaer.