func BenchmarkIsPalindrome(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
IsPalindrome("A man, a plan, a canal: Panama")
}
}
效果:
pkg: gopl.io/ch11/word2 BenchmarkIsPalindrome-8 2151261 534 ns/op 248 B/op 5 allocs/op PASS
func BenchmarkIsPalindrome(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
IsPalindrome("A man, a plan, a canal: Panama")
}
}
效果:
pkg: gopl.io/ch11/word2 BenchmarkIsPalindrome-8 2151261 534 ns/op 248 B/op 5 allocs/op PASS
go build -trimpath -ldflags "-s -w" test.go参考链接:https://niconiconi.fun/2019/01/14/reduce-go-binary-file-size/
![]() |
| 保存网页失败 |
![]() |
| 连续的失败 |
<script src="https://cdn.rawgit.com/google/code-prettify/master/loader/run_prettify.js"></script>2. 在“主题背景” --> “自定义” --> “高级” --> “添加CSS” --> 添加如下代码;
.post .code {
display: block; /* fixes a strange ie margin bug */
font-family: Courier New;
font-size: 10pt;
overflow:auto;
background: #f0f0f0 url(data:image/gif;base64,iVBORw0KGgoAAAANSUhEUgAAAAsAAASwCAYAAAAt7rCDAAAABHNCSVQICAgIfAhkiAAAAQJJREFUeJzt0kEKhDAMBdA4zFmbM+W0upqFOhXrDILwsimFR5pfMrXW5jhZr7PwRlxVX8//jNHrGhExjXzdu9c5IiIz+7iqVmB7Hwp4OMa2nhhwN/PRGEMBh3Zjt6KfpzPztxW9MSAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzB8HS+J9kUTvzEDMwAAAABJRU5ErkJggg==) left top repeat-y;
border: 1px solid #ccc;
padding: 10px 10px 10px 21px;
max-height:200px;
line-height: 1.2em;
}
3. 最后在HTML编辑模式下添加自己的代码,使用下面的格式:
<pre class="code prettyprint"> some code </pre>
fibonacci function that returns a function (a closure) that
returns successive fibonacci numbers
(0, 1, 1, 2, 3, 5, ...).
package main
import "fmt"
// fibonacci is a function that returns
// a function that returns an int.
func fibonacci() func() int {
a := 0
b := 1
return func() int {
a, b = b, a+b
return b - a
}
}
func main() {
f := fibonacci()
for i := 0; i < 10; i++ {
fmt.Println(f())
}
}
WordCount. It should return a map of the counts of each “word” in the string s. The wc.Test function runs a test suite against the provided function and prints success or failure.
package main
import (
"golang.org/x/tour/wc" "strings")
func WordCount(s string) map[string]int {
res := make(map[string]int)
arr := strings.Split(s, " ")
for _, v := range arr {
if _, ok := res[v]; !ok {
res[v] = 1 } else {
res[v] ++
}
}
return res}
func main() {
wc.Test(WordCount)
}
运行输出:
PASS
f("I am learning Go!") =
map[string]int{"Go!":1, "I":1, "am":1, "learning":1}
PASS
f("The quick brown fox jumped over the lazy dog.") =
map[string]int{"The":1, "brown":1, "dog.":1, "fox":1, "jumped":1, "lazy":1, "over":1, "quick":1, "the":1}
PASS
f("I ate a donut. Then I ate another donut.") =
map[string]int{"I":2, "Then":1, "a":1, "another":1, "ate":2, "donut.":2}
PASS
f("A man a plan a canal panama.") =
map[string]int{"A":1, "a":2, "canal":1, "man":1, "panama.":1, "plan":1}
func newInt *int {
var i int
return &i //为何可以返回局部变量呢?
}
someInt := newInt()
在Go中变量或者对象的保存不一定必须在栈或者堆中的,Go语言会根据变量在函数中的生命周期有关系,如果变量仅在函数中引用,那么就会保存在栈中,函数结束变量从栈中移除。如果函数中的变量不只是在函数中使用,比如列子中的,那么会将变量保存在堆中,所以列子中的变量是可以返回的,这就是Go的变量逃逸。