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的更多相关文章

  1. Atitit HTTP 认证机制基本验证 (Basic Authentication) 和摘要验证 (Digest Authentication)attilax总结

    Atitit HTTP认证机制基本验证 (Basic Authentication) 和摘要验证 (Digest Authentication)attilax总结 1.1. 最广泛使用的是基本验证 ( ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 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#? ...

  9. 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 ...

  10. 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 ...

随机推荐

  1. git彻底删除某个文件的全部log历史记录

    git filter-branch -f --tree-filter 'rm -rf vendor/gems' HEAD git push origin --force如果log历史较多 可能需要一点 ...

  2. Emgucv - ImageBox控件

    C#的Winform中用来显示图片的控件是PictureBox,而Emgucv中显示图片用的是ImageBox. 1.ImageBox属性 在WinForm中,单击ImageBox控件,右击查看属性页 ...

  3. 编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第5章编程练习9

    #include <iostream>using namespace std;int main(){ int num; cout<<"Enter number of ...

  4. spring-boot-maven-plugin 安装本地jar 包

    本地使用nexus 进行maven仓库管理.项目deploy 引入之后,总是找不到jar中定义的class或者配置文件等. 从截图上可以看到虽然class文件是有的,但是引用的时候却是找不到的. Sp ...

  5. Java当中的IO一

    1.IO操作的目标 什么是IO操作的目标? 输入: 从数据源当中读取数据 输出: 将数据写入到数据目的地当中 有数据进入到程序当中,这个过程就可以被叫做输入 流:即在数据源与程序之间建立的传输通道 2 ...

  6. MongoDB_简介_安装_基本使用_js_mongoose 操作 MongoDB 编程

    数据库 按照数据结构来组织.存储和管理数据的仓库 程序运行时,数据存储于内存中,一旦程序结束或者断电,就会数据丢失 为了将有些数据持久化存储到硬盘中,并确保可操作性和安全性,就需要数据库 分类: 关系 ...

  7. 傅里叶变换及其应用讲义(stanford_ee261)

    链接:http://pan.baidu.com/s/1bprVIqF 密码:q5iv

  8. jquery常用语句

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. 如何查询注册表的值及 Powershell 应用

    利用 c:\windows\system32\reg.exe 的 query 参数即可. reg.exe 的参数如下: C:\windows\system32> reg.exe /?REG Op ...

  10. Google Adsense Google判断广告点击作弊的方式和数据 数据分析

    Google判断广告点击作弊的几种方式和数据 - 王庆东mas - 博客园 http://www.cnblogs.com/x-poior/p/5581327.html 作弊广告点击的CTR数据太高网上 ...