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 ...
随机推荐
- 重新学习Java的开始~
安装jdk的步骤及解释已经在这篇文章中详细阐述了,如下: http://www.cnblogs.com/godtrue/p/4338323.html 1.如何安装库源文件--摘自coreJava 库源 ...
- luogu P3952 时间复杂度 模拟
题目链接 luogu P3952 时间复杂度 题解 直接模拟即可 注意不要直接return 我真是naive ...... 代码 #include<map> #include<sta ...
- python实现的跳点寻路算法(JPS)
原理参考论文 代码已提交到git(https://github.com/YYRise/find_path/blob/master/jps.py)
- python之面向对象深入探测
一 __doc__ 表示类的描述信息 #!/usr/bin/env python # -*- coding: utf-8 -*- class C: """ 这是个大美女 ...
- Nginx配置:nginx如何配置跳转fpm
location ~ \.php(.*)$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_split_path_inf ...
- Java 初始化、final、清理
1 为什么需要无参构造器? 第一个是继承需要 super 调用父类的构造器(父类构造器必须存在且不为 private.可以是无参/默认构造器,也可以是有参构造器),特别的如果父类不包含无参构造器的话, ...
- 梦里寻她千百度,Bug却在隔壁老张处
程序员与 Bug 是一对矛盾的存在,程序员既要在解决 Bug 中获得成就感,同时也讨厌 Bug 本身的存在.“程序不息,Bug 不止”,程序员在与 Bug 的斗争中,也有很多有趣的事情发生,我们整理了 ...
- Python学习小纪
1.打包发布*.py文件---"文件路径下打开命令行 d:\python\python.exe setup.py sdist" eg:打包发布f:\C\python\print_l ...
- c++ 库函数cmath
cmath中常用库函数: int abs(int i);//返回整型参数i的绝对值double fabs(double x);//返回双精度参数x的绝对值long labs(long n);//返回长 ...
- 遍历其 interator
当用户自定义数据结构的时候, 如果做遍历呢, 这个时候就需要遍历器 interator 了, 它允许用户定义遍历自己自定义数据结构的方式 当用户定义了iterator, 就可以使用 for of 来遍 ...