首页 > 其他分享 >Golang基础-格式化输出

Golang基础-格式化输出

时间:2023-02-18 21:33:56浏览次数:39  
标签:case 输出 格式化 notation 16 default %# Golang base

General

%v	the value in a default format
	when printing structs, the plus flag (%+v) adds field names
%#v	a Go-syntax representation of the value
%T	a Go-syntax representation of the type of the value
%%	a literal percent sign; consumes no value

Boolean

%t	the word true or false

Integer

%b	base 2
%o	base 8
%O	base 8 with 0o prefix
%d	base 10
%x	base 16, with lower-case letters for a-f
%X	base 16, with upper-case letters for A-F
%c	the character represented by the corresponding Unicode code point
%q	a single-quoted character literal safely escaped with Go syntax.
%U	Unicode format: U+1234; same as "U+%04X"

Floating-point and complex constituents

%b	decimalless scientific notation with exponent a power of two,
	in the manner of strconv.FormatFloat with the 'b' format,
	e.g. -123456p-78
%e	scientific notation, e.g. -1.234456e+78
%E	scientific notation, e.g. -1.234456E+78
%f	decimal point but no exponent, e.g. 123.456
%F	synonym for %f
%g	%e for large exponents, %f otherwise. Precision is discussed below.
%G	%E for large exponents, %F otherwise
%x	hexadecimal notation (with decimal power of two exponent), e.g. -0x1.23abcp+20
%X	upper-case hexadecimal notation, e.g. -0X1.23ABCP+20

String and slice of bytes (treated equivalently with these verbs)

%s	the uninterpreted bytes of the string or slice
%q	a double-quoted string safely escaped with Go syntax
%x	base 16, lower-case, two characters per byte
%X	base 16, upper-case, two characters per byte

Slice

%p	address of 0th element in base 16 notation, with leading 0x

Pointer

%p	base 16 notation, with leading 0x
The %b, %d, %o, %x and %X verbs also work with pointers,
formatting the value exactly as if it were an integer.

The default format for %v is:

bool:                    %t
int, int8 etc.:          %d
uint, uint8 etc.:        %d, %#x if printed with %#v
float32, complex64, etc: %g
string:                  %s
chan:                    %p
pointer:                 %p

宽度和精度控制

%f     default width, default precision
%9f    width 9, default precision
%.2f   default width, precision 2
%9.2f  width 9, precision 2
%9.f   width 9, precision 0

Other flags:

'+'	always print a sign for numeric values;
	guarantee ASCII-only output for %q (%+q)
'-'	pad with spaces on the right rather than the left (left-justify the field)
'#'	alternate format: add leading 0b for binary (%#b), 0 for octal (%#o),
	0x or 0X for hex (%#x or %#X); suppress 0x for %p (%#p);
	for %q, print a raw (backquoted) string if strconv.CanBackquote
	returns true;
	always print a decimal point for %e, %E, %f, %F, %g and %G;
	do not remove trailing zeros for %g and %G;
	write e.g. U+0078 'x' if the character is printable for %U (%#U).
' '	(space) leave a space for elided sign in numbers (% d);
	put spaces between bytes printing strings or slices in hex (% x, % X)
'0'	pad with leading zeros rather than spaces;
	for numbers, this moves the padding after the sign;
	ignored for strings, byte slices and byte arrays

标签:case,输出,格式化,notation,16,default,%#,Golang,base
From: https://www.cnblogs.com/roadwide/p/17133683.html

相关文章

  • golang 生成swagger 文档
     github:https://github.com/swaggo/swag/blob/master/README_zh-CN.md 参考:https://www.cnblogs.com/cxykhaos/p/15345317.html 第一步:下载swag命令:goinstall......
  • Golang基础-字符串
    StringsAstringinGoisanimmutable(不可变的)sequenceofbytes,whichdon'tnecessarilyhavetorepresentcharacters.doublequotes("")和backticks(`)的区别......
  • Golang基础-Basics
    PackagesGo语言中的包和其他语言的库或模块的概念类似,目的都是为了支持模块化、封装、单独编译和代码重用。一个包的源代码保存在一个或多个以.go为文件后缀名的源文件中,......
  • Jenkins控制台输出显示颜色
    安装插件【AnsiColor】,在项目配置构建环境颜色例子:echo-e"\033[41;30m红底黑字\033[0m"echo-e"\033[30m黑色字\033[0m"echo-e"\033[31m红色字\033[0m"echo-e......
  • golang流程控制if,switch分支
    if分支if单分支if条件表达式{逻辑代码}packagemainimport"fmt"funcmain(){ //varaint=9 //ifa<10{//判断a《10位true,所以为执行下面的打印a的......
  • PAT-basic-1006 换个格式输出整数 java
    一、题目让我们用字母B来表示“百”、字母S表示“十”,用12...n来表示不为零的个位数字n(<10),换个格式来输出任一个不超过3位的正整数。例如234应该被输出为BBSS......
  • 62-CICD持续集成工具-Jenkins构建Golang的web项目
    实现Golang应用源码编译并部署安装Golang环境#编译安装[root@jenkins~]#catinstall_go.sh#!/bin/bashGO_VERSION=1.18.4URL=https://studygolang.com/dl/golang/go${......
  • C语言填空:逆序输出任意字符串
    #include<stdio.h>//逆序输出任意字符串【1】main(){charzf[100];inta,b;【2】;a=【3】;for(b=a-1;b>=0;b--)printf("%c",zf[b]);......
  • 算法刷题-只出现一次的数字、输出每天是应该学习还是休息还是锻炼、将有序数组转换为
    只出现一次的数字(位运算、数组)给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次。找出那个只出现了一次的元素。说明:你的算法应该具有线性时间复......
  • PHP输出13位时间戳函数
    functiongetUnixTimestamp(){list($s1,$s2)=explode('',microtime());return(float)sprintf('%.0f',(floatval($s1)+floatval($s2))*1000);}......