我爱占星网 我爱占星网
首页
编程
java
php
前端
首页 编程 java php 前端

golang格式,带符号的前导零

I'd like to format an integer to an UTC offset formatted string

I tried it using the fmt package:

fmt.Sprintf("%+02d:00", utc)

When utc is 1, I'd like it to print "+01:00", but I get "+1:00"
How can I combine the leading zeros flag, the sign flag and the width in one format string?

width is the minimum number of runes to output

+01 is minimum width 3. For example,

package main

import (
    "fmt"
)

func main() {
    utc := 1
    s := fmt.Sprintf("%+03d:00", utc)
    fmt.Println(s)
}

Playground: https://play.golang.org/p/Z0vBzzn-kp

Output:

+01:00

近期文章

  • 如何在Go中声明gRPC错误代码客户端
  • 具有后备功能的汇编功能实现
  • 使用定义中的接口的Go接口
  • Golang-解析yaml文件并检查对象的单元测试
  • Go REST Api中的角色和权限
  • 使用Goroutine实际上需要更长的时间才能执行
  • golang log.Printf崩溃
  • 如何从.kube / config中设置的当前上下文获取名称空间
  • 无法生成或安装GO工具二进制文件
  • 打开切片切片
  • golang XML结束解析并显示“无效的UTF-8”错误
  • 请问下python里种群竞争模型的代码需要怎么写
  • 格式化Go字符串而不打印?
  • 如何在Go中处理配置
  • 如何将字符串分配给字节数组
  • Go中的:=和=运算符之间的区别
  • 使用json.Unmarshal与json.NewDecoder.Decode解码JSON
  • Go中标签的用途是什么?
  • 求解决java 怎么打包成jar包问题
  • 函数声明语法:函数名称前括号内的内容

Copyright ©2022 我爱占星 All Rights Reserved.

浙ICP备2022030071号-1

部分图文来自网络,如有侵犯您的版权,请告诉我们删除

友情链接:代码精华