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 ...
随机推荐
- 自定义MVC实现登录案例
MVC框架: MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,一种软件设计典范,用一种业务逻辑.数据.界面显示分离 ...
- 【转】Spring总结以及在面试中的一些问题
[转]Spring总结以及在面试中的一些问题. 1.谈谈你对spring IOC和DI的理解,它们有什么区别? IoC Inverse of Control 反转控制的概念,就是将原本在程序中手动创建 ...
- HDU 2008 数值统计
题目链接:HDU 2008 Description 统计给定的n个数中,负数.零和正数的个数. Input 输入数据有多组,每组占一行,每行的第一个数是整数n(n<100),表示需要统计的数值的 ...
- Oracle命令行中显示:ORA-04076: 无效的 NEW 或 OLD 说明
Oracle命令行进行操作时可能出现"ORA-04076: 无效的 NEW 或 OLD 说明" 需要在条件语句中JOB前面添加“old.”即可(因为是在when条件里面,所以不用“ ...
- js图片预加载与延迟加载
图片预加载的机制原理:就是提前加载出图片来,给前端的服务器有一定的压力. 图片延迟加载的原理:为了缓解前端服务器的压力,延缓加载图片,符合条件的时候再加载图片,当然不符合的条件就不加载图片. 预加载 ...
- PL-SVO公式推导及代码解析:位姿优化
通过跳过极线约束单独优化图像中每个特征的位置后,必须通过最小化3D特征与图像中相应的2D特征位置之间的重投影误差来进一步细化(3)中获得的相机姿态( 见图5).为此,我们考虑在世界坐标系中3D特征和相 ...
- 阿里云服务器ssh经常一段时间就断掉解决办法
#vim /etc/ssh/sshd_config 找到下面两行 #ClientAliveInterval 0#ClientAliveCountMax 3 去掉注释,改成 ClientAliveInt ...
- Digest of Overview of Linux Kernel Security Features
Linux kernel Security: I. DAC: Discretionary Access Control, the core security model of UNIX. II. PO ...
- :before和::before的区别
单冒号(:)用于CSS3伪类,双冒号(::)用于CSS3伪元素.伪元素和伪类之所以这么容易混淆,是因为他们的效果类似而且写法相仿,但实际上 css3 为了区分两者,已经明确规定了伪类用一个冒号来表示, ...
- [daily][netcat] 在UNIX socket上使用netcat
概述 默认情况下,系统里边带的netcat,也就是nc.支持tcp,udp,ipv4,ipv6但是不支持unix socket. 而且,telnet也不支持. 除非自己写一个,不然很不方便. 另一个n ...