Go语言中文网 为您找到相关结果 64

golang -- 时间日期总结

golang时间处理 相关包 "time" 时间戳 当前时间戳 fmt.Println(time.Now().Unix()) # 1389058332 str格式化时间 当前格式化时间 fmt.Println(time.Now().Format("2006-01-02 15:04:05")) # 这是个奇葩,必须是这个时间点, 据说是go诞生之日, 记忆方法:6-1-2-3-4-5 # 2014-01-07 09:42:20 时间戳转str格式化时间str_time := time.Unix(1389058332, 0).Format("2006-01-02 15:04:05") fmt.Println(str_time) # 2014-01-07 09:32:12 str格式化时间转时间戳...阅读全文

博文 2014-10-04 19:26:54 flyking

golang学习的点点滴滴:range使用总结

1、当range作用于string时, 第一个返回值为index,第二次是char str := "hello world" for index, ch := range str { fmt.Printf("%d --- %c\n", index, ch) } 2、当range作用于array时, 第一个返回值为index,第二次是value func PrintArray(array [5]int) { for index, res := range array { fmt.Println(index, "--", res) } } func main() { array := [5]int{1,2,3,4,5} PrintArray(array) } 3、当range作用于slice时,...阅读全文

博文 2014-10-04 19:27:41 亓斌哥哥

command-line arguments _ golang

Command-line arguments are a common way to parameterize execution of programs. For example, go run hello.go uses run and hello.go arguments to the go program package main import ( "fmt" "os" ) func main() { argsWithProg := os.Args argsWithoutProg := os.Args[1:] arg := os.Args[3] fmt.Println(argsWithProg) fmt.Println(argsWithoutProg) fmt.Println(arg...阅读全文

博文 2015-04-01 03:00:00 jackkiexu

2019年6月复盘

柳传志复盘方法论 1、回顾目标:当初目的或期望是什么 2、评估结果:和原定目标相比有哪些亮点和不足 3、分析原因:事情成功和失败根本原因,包括主观和客观两方面 4、总结经验:需要实施哪些新举措,叫停哪些项目等 本月周复盘汇总 2019第23/52周复盘 2019第24/52周复盘 2019第25/52周复盘 2019第26/52周复盘 1、6月计划完成情况 []《论语》每周写/2篇解读。 [] 跟上混沌商学院的课程,参与线下活动。 [] 完成论证分析的大作业,做成论证分析的知识卡片。 [x] 跑步52/50公里。 [] 完成深入拆解Java虚拟机。 [] /5篇周期文章的编辑。 6月书单:跑步圣经、跑步成为最好的自己、跑步的197条守则、跨界学习、穷查理年鉴、你一定要读的50部投资学经典 这...阅读全文

博文 2019-06-30 18:32:41 空灵一月

golang开发博客的一些经验总结

原文链接:[https://ashan.org/archives/932](https://ashan.org/archives/932) 国庆节时,用大约9天时间借助golang重写编写了博客系统的前台部分,由于系统的特殊性,这部分完成后只能说完成了整套系统1/3的功能。十一回来后,又用了大约10天时间,每天写一点,完成了系统余下的两部分内容。由于是第一次用golang开发服务器内容,所以踩得坑不少,这里做一个总结。 ### 函数传值问题 这个问题不算难,一开始看文档的时候已经注意到了,但没太在意。因为很多逻辑代码都是照搬旧版Nodejs版本来写的,所以就更加忽略了这个问题。知道本地命令操作时间略长,我才感觉不对劲。 举一个简单的例子: ``` pack...阅读全文

range over channel _ golang

In a previous example we saw how for and range provide iteration over basic data structures. We can alse use this syntax to iterate over values received from a channel package main import ( "fmt" ) func main() { queue := make(chan string, 2) queue <- "one" queue <- "two" close(queue) for elem := range queue { fmt.Println(elem) } } one two 总结 : 1 : ...阅读全文

博文 2015-03-22 15:00:01 jackkiexu

map _ golang

Maps are Go's built-in associative data type(sometimes called hashes or dits in other languages) package main import ( "fmt" ) func main() { m := make(map[string]int) m["k1"] = 7 m["k2"] = 13 fmt.Println("map :", m) v1 := m["k1"] fmt.Println("v1 :", v1) fmt.Println("len : ", len(m)) delete(m, "k2") fmt.Println("map:", m) a, prs := m["k2"] fmt.Print...阅读全文

博文 2015-03-14 03:00:01 jackkiexu

2019将至,我的Java年终总结(六年开发经验),请查收

恍然间,发现自己在这个行业里已经摸爬滚打了六年了,原以为自己就凭已有的项目经验和工作经历怎么着也应该算得上是一个业内比较资历的人士了,但是今年在换工作的过程中却遭到了重大的挫折。详细过程我就不再叙述,在此,只想给大家说一说我的总结吧,看看大家有没有相似的经历,和类似的感悟。 ![image.png](https://static.studygolang.com/181226/86ef8b178c4fa08d1a254b8ea90f3b61.png) 第一. Java程序员需要不断的学习; 貌似这一点适应的行业最广,但是我可以很肯定的说:当你从事web开发一年后,重新找工作时,才会真实的感受到这句话。 工作第一年,往往是什么都充满新鲜感,什么都学习,冲劲十足的一年;WEB行业知识更新特别快,今...阅读全文

博文 2018-12-26 20:26:35 Javaspring12

rate limiting _ golang

Rate limiting is an import mechanism for controlling resource utilzation and maintaining quality of service. Go elegantly supports rate with goroutines, channels, and tickers. package main import ( "fmt" "time" ) func main() { requests := make(chan int, 5) for i := 1; i <= 5; i++ { requests <- i } close(requests) limiter := time.Tick(time.Milliseco...阅读全文

博文 2015-03-20 03:00:01 jackkiexu

slices _ golang

Slices are a key data type in Go, giving a more powerful interface to sequences than arrays package main import ( "fmt" ) func main() { s := make([]string, 3) fmt.Println("emp:", s) s[0] = "a" s[1] = "b" s[2] = "c" fmt.Println("set : ", s) fmt.Println("get : ", s[2]) fmt.Println("len :", len(s)) s = append(s, "d") s = append(s, "e", "f") fmt.Printl...阅读全文

博文 2015-03-14 03:00:00 jackkiexu

execing process _ golang

In the previous example we looked at spawning external processes. We do this when we need an external process accessible to running Go process. Sometimes we just want to completely replace the current Go process with another one. To do this we'll use Go's implementation of classic exec function package main import ( "os" "os/exec" "syscall" ) func ...阅读全文

博文 2015-04-02 03:00:07 jackkiexu

constants _ golang

golang 中支持 长量 const or static package main import ( "fmt" "math" ) const s string = "constant" const a = 3000 func main() { const s = 3000 fmt.Println(s) const n = (3e8 / s) fmt.Println(n) fmt.Println(int64(n)) fmt.Println(math.Sin(n)) } 3000 100000 100000 0.03574879797201651 总结 : 1 : const 常量,"const n = 3000" 这样写是ok的,但是 "n = 3000" 或 "const n := 30...阅读全文

博文 2015-03-12 03:00:00 jackkiexu

Spawning process _ golang

Sometimes our Go programs need to spawn other, non-Go process. For example, the syntax highlighting on this site is implemented by spawning a pygmentize process from a Go program. Let's look at a few examples of spawning processes from Go package main import ( "fmt" "io/ioutil" "os/exec" ) func main() { dateCmd := exec.Command("date") dateOut, err ...阅读全文

博文 2015-04-02 03:00:07 jackkiexu

URL parsing _ golang

URLs provide a uniform way to locate resources. Here's how to pare URLs in Go package main import ( "fmt" "net" "net/url" ) func main() { s := "postgres://user:pass@host.com:5432/path?k=v#f" u, err := url.Parse(s) if err != nil { panic(err) } fmt.Println(u.Scheme) fmt.Println(u.User) fmt.Println(u.User.Username()) p, _ := u.User.Password() fmt.Prin...阅读全文

博文 2015-03-28 03:00:03 jackkiexu

if else _ golang

if else 在 golang package main import ( "fmt" ) func main() { if 7%2 == 0 { fmt.Println("7 is even") } else { fmt.Println("7 is odd") } if 8%4 == 0 { fmt.Println("8 is divisible by 4") } if num := 9; num < 0 { fmt.Println(num, "is negative") } else if num < 10 { fmt.Println(num, "has 1 digit") } else { fmt.Println(num, "has multiple digits") } } 7 i...阅读全文

博文 2015-03-13 03:00:01 jackkiexu

Random numbers _ golang

Go's math/rand package provides pseudorandom number generation package main import ( "fmt" "math/rand" ) func main() { fmt.Print(rand.Intn(100), ",") fmt.Println() fmt.Println(rand.Float64()) fmt.Println((rand.Float64()*5)+5, ",") s1 := rand.NewSource(42) r1 := rand.New(s1) fmt.Println(r1.Intn(100)) s2 := rand.NewSource(42) r2 := rand.New(s2) fmt.P...阅读全文

博文 2015-03-27 04:00:06 jackkiexu

go相关书评

1、Go web编程 快速入门,用beego框架可以快速做一个网站(不上班状态,1周看完) 2、Go 并发编程 非常详细,打基础专用 (不上班状态,3周看完) 3、Go语言程序设计 各种经验总结点评,都是干货 (上班状态,每天3小时,2周看完) "Go语言程序设计" 之于 "Go并发编程", 就像"Essential C++" 之于 "C++ Primer " 前者都是高屋建瓴,最好是先看后者再看前...阅读全文

博文 2015-11-27 10:00:10 wk3368

workerPool _ golang

In this example we'll look at how to implement a worker pool using goroutines and channels package main import ( "fmt" "time" ) func worker(id int, jobs <-chan int, result chan<- int) { for j := range jobs { fmt.Println("worker", id, "processing job", j) time.Sleep(time.Second) result <- j * 2 } } func main() { jobs := make(chan int, 100) result :=...阅读全文

博文 2015-03-19 03:00:01 jackkiexu

base64 encoding _ golang

This syntax imports the encoding/base64 package with the b64 name instead of the default 64. It'll save us some space below package main import ( b64 "encoding/base64" "fmt" ) func main() { data := "abc123!?$*&()'-=@~" sEnc := b64.StdEncoding.EncodeToString([]byte(data)) fmt.Println(sEnc) sDec, _ := b64.StdEncoding.DecodeString(sEnc) fmt.Println(st...阅读全文

博文 2015-03-31 03:00:01 jackkiexu

range _ golang

range iterates over of elements in variety of data structures. Let's see how use range with some of the data structures we've already leraned package main import ( "fmt" ) func main() { nums := []int{1, 2, 3} sum := 0 for a, num := range nums { sum += num fmt.Println("a:", a) } fmt.Println("sum :", sum) for i, num := range nums { if num == 3 { fmt....阅读全文

博文 2015-03-14 03:00:00 jackkiexu

environment variables _ golang

Environment variables are a univerial mechanism for conveying configuration information to Unix programs. Let's look at how to set, get, and list environmant variables package main import ( "fmt" "os" "strings" ) func main() { os.Setenv("FOO", "1") fmt.Println("FOO:", os.Getenv("FOO")) fmt.Println("BAR:", os.Getenv("BAR")) for _, e := range os.Envi...阅读全文

博文 2015-04-02 03:00:07 jackkiexu

项目经理的工具箱

PDCA PDCA循环是美国质量管理专家休哈特博士首先提出的,由戴明采纳、宣传,获得普及,所以又称戴明环。全面质量管理的思想基础和方法依据就是PDCA循环。PDCA循环的含义是将质量管理分为四个阶段,即计划(Plan)、执行(Do)、检查(Check)、处理(Act)。在质量管理活动中,要求把各项工作按照作出计划、计划实施、检查实施效果,然后将成功的纳入标准,不成功的留待下一循环去解决。这一工作方法是质量管理的基本方法,也是企业管理各项工作的一般规律。 PDCA的思想可有质量问题推广到所有问题的分析和改进。 分析现状,发现问题。 PDCA环—大环套小环 分析质量问题中各种影响因素。 找出影响质量问题的主要原因。 针对主要原因,提出解决的措施并执行。 检查执行结果是否达到了预定的目标。 把成功...阅读全文

time formatting _ golang

Go supports time formatting and parsing via pattern-based layouts package main import ( "fmt" "time" ) func main() { p := fmt.Println t := time.Now() p(t.Format(time.RFC3339)) t1, e := time.Parse(time.RFC3339, "2012-11-01T22:08:41+00:00") p(t1) p(e) p(t.Format("3:04PM")) p(t.Format("Mon Jan _2 15:04:05 2006")) p(t.Format("2006-01-02T15:04:05.999999...阅读全文

博文 2015-03-27 04:00:05 jackkiexu

closures _ golang

Go supports anonymous functions, which can form closures. Anonymous functions are useful when you want to define a function inline without having to name it package main import ( "fmt" ) func intSeq() func() int { i := 0 return func() int { i += 1 return i } } func main() { nextInt := intSeq() fmt.Println(nextInt()) fmt.Println(nextInt()) fmt.Print...阅读全文

博文 2015-03-15 10:00:01 jackkiexu

sorting _ golang

Go's sort package implements sorting for builtins and user-defined types. We'll look at sorting for builtins first package main import ( "fmt" "sort" ) func main() { strs := []string{"c", "a", "b"} sort.Strings(strs) fmt.Println("Strings:", strs) ints := []int{7, 2, 4} sort.Ints(ints) fmt.Println("Ints: ", ints) s := sort.IntsAreSorted(ints) fmt.Pr...阅读全文

博文 2015-03-21 03:00:01 jackkiexu

collection function _ golang

We often need our programs to perform operations on collection of data, like selecting all items that satisfy a given predicate or mapping all items to a new collection with a custom function. package main import ( "fmt" "strings" ) func Index(vs []string, t string) int { for i, v := range vs { if v == t { return i } } return -1 } func Include(vs [...阅读全文

博文 2015-03-24 03:00:01 jackkiexu

Reading files _ golang

Reading and writing files are basic tasks needed for many Go programs. First we'll look at some examples of reading files package main import ( "bufio" "fmt" "io" "io/ioutil" "os" ) func check(e error) { if e != nil { panic(e) } } func main() { dat, err := ioutil.ReadFile("/tmp/dat") check(err) fmt.Println(string(dat)) f, err := os.Open("/tmp/dat")...阅读全文

博文 2015-03-31 03:00:01 jackkiexu

non-blocking channel options _ golang

Basic sends and receives on channels are blocking. However, we can use select with a default clause to implement non-blocking sends, receives, and even non-blocking multi-way selects package main import ( "fmt" ) func main() { message := make(chan string) signals := make(chan bool) select { case msg := <-message: fmt.Println("received message", msg...阅读全文

博文 2015-03-23 14:00:00 jackkiexu

string functions _ golang

The standard libarary's strings package provides many useful string-related functions. Here are some examples to give you a sense of the package package main import "fmt" import s "strings" var p = fmt.Println func main() { p("Contains: ", s.Contains("tests", "es")) p("Count: ", s.Count("test", "t")) p("HasPrefix: ", s.HasPrefix("test", "te")) p("H...阅读全文

博文 2015-03-25 03:00:01 jackkiexu

epoch _ golang

A common requirement in programs is getting the number of seconds, millisecond, or nanoseconds since Unix epoch. Here's how to do it in Go package main import ( "fmt" "time" ) func main() { now := time.Now() secs := now.Unix() nanos := now.UnixNano() fmt.Println(now) millis := nanos / 1000000 fmt.Println(secs) fmt.Println(millis) fmt.Println(nanos)...阅读全文

博文 2015-03-27 04:00:06 jackkiexu

channel _ golang

Channels are the pipes that connect concurrent goroutines. You can send values into channels from one goroutine andreceive those values into another goroutine package main import ( "fmt" ) func main() { messages := make(chan string) go func() { messages <- "ping" }() msg := <-messages fmt.Println(msg) } ping 总结 : 1 : .........阅读全文

博文 2015-03-16 03:00:01 jackkiexu

atomic counters _ golang

The primary mechanism for managing state in Go is communication over channels. We saw this for example with worker pools. There are a few other options for managing state though. Here we'll look at using the sync/atomic package for atomic counters accessed by multiple goroutines package main import ( "fmt" "runtime" "sync/atomic" "time" ) func main...阅读全文

博文 2015-03-20 03:00:01 jackkiexu

Go语言程序设计.epub

【下载地址】国外最经典的Go语言著作,Go语言编程的先驱者Mark Summerfield的实践经验总结。这是一本Go语言实战指南,帮你了解Go语言,按Go语言的方式思考,以及使用Go语言来编写高性能软件。作者展示了如何编写充分利用Go语言突破性的特性和惯用法的代码,以及Go语言在其他语言之上所做的改进,并着重强调了Go语言的关键创新。注重实践教学,每章都提供了多个经过精心设计的代码示例。由国内第一个核心服务完全采用Go语言实现的团队——七牛团队核心成员翻译...阅读全文

common-line flags _ golang

Command-line flags are a common way to specify options for command-line programs. For eample, in wc -l the -l is a command-line flag package main import ( "flag" "fmt" ) func main() { wordPtr := flag.String("word", "foo", "a string") numbPtr := flag.Int("numb", 42, "an int") boolPtr := flag.Bool("fork", false, "a bool") var svar string flag.StringV...阅读全文

博文 2015-04-01 03:00:00 jackkiexu

statefule goroutines _ golang

In the previous example we used explicit locking with mutexes to synchronize access to shared state across multiple goroutines. Another option is to use the built-in synchronization features of goroutines and channels to achieve the same result. This channel-based approach aligns with Go's ideas of sharing memory by communivating and having each pi...阅读全文

博文 2015-03-21 03:00:01 jackkiexu

line filters _ golang

A line filter is a common type of program that reads input on stdin, processes it, and then prints some derived result to stdout. grep and sed are common line filters package main import ( "bufio" "fmt" "os" "strings" ) func main() { scanner := bufio.NewScanner(os.Stdin) for scanner.Scan() { ucl := strings.ToUpper(scanner.Text()) fmt.Println(ucl) }...阅读全文

博文 2015-04-01 03:00:01 jackkiexu

interface _ golang

Interfaces are named collections of methods signatures package main import ( "fmt" "math" ) type geometry interface { area() float64 perim() float64 } type square struct { width, height float64 } type circle struct { radius float64 } func (s square) area() float64 { return s.width * s.height } func (s square) perim() float64 { return 2*s.width + 2*...阅读全文

博文 2015-03-15 10:00:01 jackkiexu

Tickers _ golang

Timers are for when you want to do something once in the future - tickers are for when you want to do something repeatedly at regular intervals. Here'an example of a ticker that ticks periodically until we stop it package main import ( "fmt" "time" ) func main() { ticker := time.NewTicker(time.Millisecond * 500) go func() { for t := range ticker.C ...阅读全文

博文 2015-03-19 03:00:00 jackkiexu

sorting functions _ golang

Sometimes we'll want to sort a collection by something other than its natural order. For example, suppose we wanted to sort strings by their length instead of alphabetically. Here's an example of custom sorts in Go package main import ( "fmt" "sort" ) type ByLength []string func (s ByLength) Len() int { return len(s) } func (s ByLength) Swap(i, j i...阅读全文

博文 2015-03-21 18:00:01 jackkiexu

writing files _ golang

Writing files in Go follows similar patterns to the ones we saw earlier for reading package main import ( "bufio" "fmt" "io/ioutil" "os" ) func check(e error) { if e != nil { panic(e) } } func main() { d1 := []byte("hello\ngo\n") err := ioutil.WriteFile("/tmp/dat", d1, 0644) check(err) f, err := os.Create("/tmp/dat") check(err) defer f.Close() d2 :...阅读全文

博文 2015-03-31 03:00:01 jackkiexu

mutexes _ golang

In the previous example we saw how to manage simple counter state using atomic operations. For more complex state we can use a mutex to safetly access data across multiple goroutines package main import ( "fmt" "math/rand" "runtime" "sync" "sync/atomic" "time" ) func main() { var state = make(map[int]int) var mutex = &sync.Mutex{} var ops int64 = 0...阅读全文

博文 2015-03-20 03:00:01 jackkiexu

defer _ golang

Defer is used to ensure that a function call is performed later in a program's execution, usually for purposes of cleanup. defer is often used wheree.g.ensure and finally would be used in other languages. package main import ( "fmt" "os" ) func createFile(p string) *os.File { fmt.Println("creating") f, err := os.Create(p) if err != nil { panic(err)...阅读全文

博文 2015-03-24 03:00:00 jackkiexu

SHA1 Hashes _ golang

SHA1 hashes are frequently used to compute short identities for binary or text blobs. For example, the git revision control system uses SHA1 extensively to identify versioned files and directories. Here's how to compute SHA1 hashes in Go package main import ( "crypto/sha1" "fmt" ) func main() { s := "sha1 this string" h := sha1.New() h.Write([]byte...阅读全文

博文 2015-03-28 03:00:03 jackkiexu

signals _ golang

Sometimes we'd like our Go programs to intelligently handle Unix signals. For example, we might want a server to gracefully shutdown when it receives a SIGTERM, or a command-line tool stop processing input if it receives a SIGINT. Here's how to handle signals in Go with channels package main import ( "fmt" "os" "os/signal" "syscall" ) func main() {...阅读全文

博文 2015-04-03 03:00:00 jackkiexu

channel directions _ golang

When using channels as function parameters, you can specify if a channel is meant to only send or receive values. This specificity increases the type-safety of the program package main import ( "fmt" ) func ping(pings chan<- string, message string) { pings <- message } func pong(pings <-chan string, pongs chan<- string) { msg := <-pings pongs <- ms...阅读全文

博文 2015-03-17 15:00:01 jackkiexu

Timers _ golang

We often want to execute Go code at some point in the future, or repeatedly at some interval. Go's built-in timer and ticker features make both od these easy. We'll look first at timers then tickers package main import ( "fmt" "time" ) func main() { timer1 := time.NewTimer(time.Second * 2) <-timer1.C fmt.Println("Time 1 expired") timer2 := time.New...阅读全文

博文 2015-03-19 03:00:01 jackkiexu