go basic
go time and rand:
package main import (
"fmt"
"math/rand"
"time"
) func main() {
rand.Seed(time.Now().Unix())
fmt.Println("My favorite number is :", rand.Int()%20)
}
get the runtime os:
package main import (
"fmt"
"runtime"
) func main() {
fmt.Println("Go runs on")
switch os := runtime.GOOS; os {
case "darwin":
fmt.Println("os x")
case "linux":
fmt.Println("Linux.")
default:
fmt.Println("windows")
}
}
slice operator
package main import (
"fmt"
// "reflect"
) func printSlice(s string, x []int) {
fmt.Printf("%s len=%d cap=%d %v\n", s, len(x), cap(x), x)
}
func main() {
var a []int
printSlice("a", a) //append works on nil slice
a = append(a, 0)
printSlice("a", a) a = append(a, 2, 3, 4)
printSlice("a", a) }
wordcount:
package main import (
"fmt"
) func WordCount(s string) map[string]int {
// c := make(map[string]int)
c := map[string]int{}
b := []byte(s)
for _, v := range b {
fmt.Println(string(v))
if cap, ok := c[string(v)]; ok {
c[string(v)] = 1 + cap
} else {
c[string(v)] = 1
}
}
return c
} func main() {
a := WordCount("wwwcmome")
fmt.Println(a)
b := map[string]int{
"name": 20,
"age": 30,
}
b["work"] = 100
if cap, ok := b["name1"]; ok {
fmt.Println(cap)
} else {
fmt.Println("nothing")
}
fmt.Println(b)
}
闭包
package main import "fmt" func adder() func(int) int {
sum := 0
return func(x int) int {
sum += x
return sum
}
} func main() {
pos, neg := adder(), adder()
for i := 0; i < 10; i++ {
fmt.Println(
pos(i),
neg(-2*i),
)
}
}
check pointer
package main import (
"fmt"
"math"
) type Abser interface {
Abs() float64
} type MyFloat float64 func (f MyFloat) Abs() float64 {
if f < 0 {
return float64(-f)
}
return float64(f)
} type Vertex struct {
x, y float64
} func (v *Vertex) Abs() float64 {
return math.Sqrt(v.x*v.x + v.y*v.y)
} func main() {
var a Abser
f := MyFloat(-math.Sqrt2)
v := Vertex{3, 4}
a = f
a = &v
fmt.Println(a.Abs())
}
strings.NewReader
package main import (
"fmt"
"io"
"strings"
) func main() {
r := strings.NewReader("hello, Reader")
b := make([]byte, 8) for {
n, err := r.Read(b)
fmt.Printf("n=%v err=%v b=%v\n", n, err, b)
fmt.Printf("b[:n] = %q\n", b[:n])
if err == io.EOF {
break
}
}
}
use golang regexp
package main import (
"fmt"
"io/ioutil"
"net/http"
"regexp"
// "strings"
// "reflect"
) func main() {
resp, err := http.Get("http://www.163.com/")
if err != nil {
fmt.Println("http get error")
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("http read error")
return
}
// fmt.Println(reflect.TypeOf(body))
src := string(body)
re, _ := regexp.Compile("http://[[:word:]]+.[[:word:]]+.com")
sslice := re.FindAllString(src, 1024)
for k, v := range removeReplaceSliceContent(sslice) {
fmt.Printf("%30s \t %5d\n", k, v)
}
} func removeReplaceSliceContent(s []string) map[string]int {
b := map[string]int{}
for _, v := range s {
b[v] += 1
}
return b
}
go basic的更多相关文章
- Atitit HTTP 认证机制基本验证 (Basic Authentication) 和摘要验证 (Digest Authentication)attilax总结
Atitit HTTP认证机制基本验证 (Basic Authentication) 和摘要验证 (Digest Authentication)attilax总结 1.1. 最广泛使用的是基本验证 ( ...
- Basic Tutorials of Redis(9) -First Edition RedisHelper
After learning the basic opreation of Redis,we should take some time to summarize the usage. And I w ...
- Basic Tutorials of Redis(8) -Transaction
Data play an important part in our project,how can we ensure correctness of the data and prevent the ...
- Basic Tutorials of Redis(7) -Publish and Subscribe
This post is mainly about the publishment and subscription in Redis.I think you may subscribe some o ...
- Basic Tutorials of Redis(6) - List
Redis's List is different from C#'s List,but similar with C#'s LinkedList.Sometimes I confuse with t ...
- Basic Tutorials of Redis(5) - Sorted Set
The last post is mainly about the unsorted set,in this post I will show you the sorted set playing a ...
- Basic Tutorials of Redis(4) -Set
This post will introduce you to some usages of Set in Redis.The Set is a unordered set,it means that ...
- Basic Tutorials of Redis(3) -Hash
When you first saw the name of Hash,what do you think?HashSet,HashTable or other data structs of C#? ...
- Basic Tutorials of Redis(2) - String
This post is mainly about how to use the commands to handle the Strings of Redis.And I will show you ...
- Basic Tutorials of Redis(1) - Install And Configure Redis
Nowaday, Redis became more and more popular , many projects use it in the cache module and the store ...
随机推荐
- Ubuntu安装并使用sogou输入法
1.下载搜狗输入法的安装包 下载地址为:http://pinyin.sogou.com/linux/ ,如下图,要选择与自己系统位数一致的安装包,我的系统是64位,所以我下载64位的安装包 2.按键C ...
- CSS(三)
CSS盒子模型 盒子模型解释 元素在页面中显示成一个方块,类似一个盒子,CSS盒子模型就是使用现实中盒子来做比喻,帮助我们设置元素对应的样式.盒子模型示意图如下: 把元素叫做盒子,设置对应的样式分别为 ...
- 面试中遇到的原生js题总结
最近面试,遇到很多js相关的面试题,总结一下. 1.js 去重 1) indexOf Array.prototype.unique = function(){ var result = []; var ...
- 有关svn的报错
由于目标计算机积极拒绝,无法连接.当报出这样的错的时候就是跨域的问题
- (51)Wangdao.com第七天_JavaScript 编写位置及输出语句
JavaScript 编写位置 编写在html内部标签的属性中 不推荐使用,因为结构和行为耦合,不便于维护 主要有 <button onclick="alert('点我干哈!');& ...
- mobile_缩放
document.documentElement.clientWidth 不包含滚动条 window.innerWidth ...
- centos7-- sbt的安装使用
sbt是一款类似于maven的构建工具 安装sbt curl https://bintray.com/sbt/rpm/rpm > bintray-sbt-rpm.repo mv bintray- ...
- [LeetCode] Max Increase to Keep City Skyline 保持城市天际线的最大增高
In a 2 dimensional array grid, each value grid[i][j] represents the height of a building located the ...
- 4、json-server的使用
json-server 详解 转载于https://www.cnblogs.com/fly_dragon/p/9150732.html JSON-Server 是一个 Node 模块,运行 Expre ...
- Java项目中启动Tomcat报错invalid LOC header
原因: 可能是jar包有问题. 解决方法: 1.找到加载不了的类对应的jar包. 2.在tomcat中webapps/INF/lib中找到对应的jar包,然后删除. 3.重新下载其它版本的jar包. ...