Go代码
开心码农1号
我很懒,我不想添加简介!
展开
-
Go语言获取当天、昨天、明天、某天0点时间戳以及格式化时间
获取当前时间 到 明天0点整的时间差的秒级时间戳的字符串形式func GetTimeDifference() string { nowTime := time.Now() // 当天秒级时间戳 nowTimeStamp := nowTime.Unix() nowTimeStr := nowTime.Format("2006-01-02") //使用Parse 默认获取为UTC时区 需要获取本地时区 所以使用ParseInLocation t2, _ := time.ParseInLoc原创 2022-04-15 11:45:09 · 13213 阅读 · 0 评论 -
Go 语言 switch 语句 获取interface变量的实际Type类型
1、可以被用于 type-switch 来判断某个 interface 变量中实际存储的变量类型。x.(type)相当于变量x的Type类型,即:reflect.TypeOf(x).Name()注意区别x的Kind类型;type BaseController struct {}func main() { var x interface{} x = BaseController{} switch i := x.(type) { case nil: fmt.Printf(" x原创 2022-04-06 19:08:56 · 2578 阅读 · 0 评论 -
Go语言中的继承与组合
package main// 父结构体type BaseController struct {}// 父结构体的方法func (c *BaseController) ReturnJson() {}// 继承type UserController struct { BaseController}// 组合type OtherController struct { userController UserController}func main() { // 调用 user.转载 2022-04-06 13:44:23 · 279 阅读 · 0 评论 -
Go语言 时间相关的转化:纳秒转 毫秒 转 英文时间格式 中文格式化
func TestTime(t *testing.T) { nowTime := time.Now() fmt.Println(nowTime) //单位:time.Time, 打印当前时间: 2021-12-17 16:00:41.5627986 +0800 CST m=+0.009973601 timeStr := nowTime.Format("2006-01-02 15:04:05") //当前时间的字符串,2006-01-02 15:04:05据说是golang的诞生时间,固定写法 f.原创 2021-12-17 16:21:03 · 1206 阅读 · 0 评论 -
Golang:实现断点续传(http单线程下载文件)
package breakpointContinuationimport ( "fmt" "io" "io/ioutil" "net/http" "os" "strconv" "strings" "testing")const ( // 断点续传下载正常响应码 NormalRespCode = "206 Partial Content" // 断点续传下载,偏移量值超文件最大值长度错误响应码 ErrRespCode = "416 Requested Range Not S.原创 2021-11-19 11:16:37 · 3058 阅读 · 0 评论 -
Go代码笔记,各字节单位
func main() { TBytes := 1 << 10 << 10 << 10 << 10 GBytes := 1 << 10 << 10 << 10 MBytes := 1 << 10 << 10 KBytes := 1 << 10 fmt.Println(TBytes) // 1099511627776 fmt.Println(GBytes) // 107374.原创 2021-10-19 14:11:56 · 178 阅读 · 0 评论 -
windows远程ssh连接linux服务器,sftp创建、读、写、删除linux路径、文件
import ( "github.com/pkg/sftp" "golang.org/x/crypto/ssh")func main() { sftpClient, err := SftpConnect("root", "mima", "192.168.xx.xx", 22) if err != nil { log.Info("creat sftpClient fail!") return } remoteFilePath := "/root/mqtt_benchmark.原创 2021-10-13 18:22:47 · 687 阅读 · 0 评论 -
Golang是如何读取文件
据需求选择:如果需要一次性读取,用ioutil.ReadFile和ioutil.ReadAll方便;如果需要分块或分行,用bufio的Read、ReadString或者ReadLine更加方便;如果是大文件读写的话,用bufio。https://www.jb51.net/article/187533.htmhttps://www.cnblogs.com/wangqianlove/p/12579994.htmlhttps://www.jianshu.com/p/62ae46556206一、一次转载 2021-08-01 01:06:03 · 334 阅读 · 0 评论 -
Golang简单写文件操作的四种方法
package mainimport ( "bufio" //缓存IO "fmt" "io" "io/ioutil" //io 工具包 "os")func check(e error) { if e != nil { panic(e) }}/** * 判断文件是否存在 存在返回 true 不存在返回false */func checkFileIsExist(filename string) bool { var exist = true if _, err :.转载 2021-07-31 20:22:52 · 3537 阅读 · 0 评论 -
Golang:实现断点续传(复制文件)
1、将文件pic_src.jpg复制到pic_des.jpg文件;2、读写过程中断时,读写的中断位置会记录在“pic_des.jpgtemp.txt”文件里;3、下次再启动程序时,程序会接着“pic_des.jpgtemp.txt”文件里记录的中断位置,继续读、写文件,直到完成传输;具体代码实现,如下:package mainimport ( "fmt" "os" "strconv" "io" "log" //"time")func main() { /* 断点续原创 2021-07-31 20:07:53 · 808 阅读 · 0 评论 -
Gochassis
1、根目录下的conf目录下的tls.yaml文件:#不使用hhttp校验的话,注释掉下面代码ssl: registry.Consumer.cipherPlugin: aes registry.Consumer.verifyPeer: fase registry.Consumer.cipherSuits: TLS_EcdHE_Rssxxxxxxxxxxxxxxx registry.Consumer.protocol: TLSv1.2 registry.Consumer.keyFi原创 2021-07-09 01:22:29 · 178 阅读 · 0 评论 -
Go语言实现定时任务
package mainimport ( "github.com/robfig/cron" "sync" "time")var mutex sync.Mutex// 任务列表var taskList []timeTask// 初始化并启动定时任务func InitProcessTimer() { c := cron.New() c.AddFunc("@every 2S", ProcessTimerTaskHandler) c.Start()}// 定时要执行的回调函.原创 2021-07-09 00:44:44 · 3373 阅读 · 0 评论