自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+
  • 博客(45)
  • 收藏
  • 关注

原创 Go基础--29 不安全编程

unsafe经常用于与C程序交互package unsafeimport ( "testing" "unsafe")func TestUnsafe(t *testing.T){ i := 10 f := *(*float64)(unsafe.Pointer(&i)) t.Logf("%T",f)}type MyInt intfunc TestConvert(t *testing.T){ a := []int{1,2,3,4} b := *(*[]MyInt)(uns

2021-06-02 22:55:30 137

原创 Go基础--28 万能程序

package reflectimport ( "reflect" "testing")func TestDeepEqual(t *testing.T){ a := map[int]string{1:"one",2:"two",3:"three"} b := map[int]string{1:"one",2:"two",4:"three"} reflect.DeepEqual(a,b) c := []int{1,2,3} d := []int{22,33,44} reflect.D

2021-06-02 22:47:57 135

原创 Go基础 -- 27 反射编程

package reflectimport ( "fmt" "reflect" "testing")func CheckType(v interface{}){ t := reflect.TypeOf(v) switch t.Kind() { case reflect.Float32,reflect.Float64: fmt.Println("Float") case reflect.Int,reflect.Int64: fmt.Println("Int") defaul.

2021-06-02 22:29:03 116

原创 Go基础--26 Benchmark

package benchmarkimport ( "testing" "time")func BenchmarkByAdd(b *testing.B){ b.ResetTimer() for i:=1;i<10;i++{ time.Sleep(1 * time.Second) } b.StopTimer()}

2021-06-02 22:04:39 85

原创 Go基础--25 单元测试

package testingimport ( "fmt" "testing")func TestCalc(t *testing.T){ inputs := [...]int{1,2,3} expected := [...]int{1,2,9} for i:=0 ; i <len(inputs);i++{ ret := calc(inputs[i]) if ret != expected[i]{ t.Errorf("input is %d,the expected i

2021-06-02 21:58:56 60

原创 Go基础--24 sync.pool

sync.pool对象缓存尝试从私有对象获取私有对象不存在,尝试从当前Processor的共享池获取()如果当前Processpr共享池也是空,那么尝试去从其他Processor的共享池获取如果所有子池都是空,最后用户指定的New函数产生一个新的对象返回GC会清除sync.pool缓存对象对象的缓存有效期为下一次GC之前package poolimport ( "fmt" "runtime" "sync" "testing")func TestSyncPool(t *te

2021-06-02 21:46:21 62

原创 Go基础--23 对象池

数据库连接,网络连接通常会将数据库池化,避免重复创建package buffer_channelimport ( "errors" "fmt" "testing" "time")type ReusableObj struct {}type ObjPool struct { bufChan chan *ReusableObj}func NewObjPool(numOfObj int) *ObjPool{ ObjPool := ObjPool{} ObjPool.bufC

2021-06-02 21:37:42 210

原创 Go基础--22 只执行一次任务

package channel_closimport ( "fmt" "sync" "testing")func dataConsumer(ch chan int,wg *sync.WaitGroup){ go func() { for i:=0 ; i<10; i++{ if data,ok := <-ch;ok{ fmt.Println(data) }else{ fmt.Println("false") } } wg.Done(

2021-05-31 20:59:27 399

原创 Go基础--22 channel关闭和广播

channel通道关闭可以通过bool值来判断package channel_closimport ( "fmt" "sync" "testing")func dataConsumer(ch chan int,wg *sync.WaitGroup){ go func() { for i:=0 ; i<10; i++{ if data,ok := <-ch;ok{ fmt.Println(data) }else{ fmt.Println("fal

2021-05-29 10:00:58 111

原创 Go基础--21 多路选择和超时

select 多路复用和超时package _selectimport ( "fmt" "testing" "time")func service() string { time.Sleep(time.Millisecond * 500) return "Done"}func AsyncService() chan string{ retch := make(chan string) go func() { ret := service() fmt.Println("

2021-05-29 00:33:33 56

原创 Go基础--20 CSP并发机制

CSP vs Actor和Actor的直接通讯不同,CSP模式则是通过Channel进行通讯,更松耦合些Go中的channel是有容量限制并且独立于处理Groutine,而如Erlang,Actor模式中的mailbox容量是无限的,接收进程也总是被动的处理消息package cspimport ( "fmt" "testing" "time")func Service() string{ time.Sleep(time.Millisecond * 50) return "D

2021-05-28 07:53:19 79

原创 Go基础--19 共享内存机制

Lock 与 waitGrouppackage share_memoryimport ( "sync" "testing" "time")func TestCounter(t *testing.T){ counter := 0 for i:=0; i<5000; i++{ go func() { counter++ }() } time.Sleep( 1 * time.Second) t.Logf("counter = %d",counter)}func

2021-05-27 22:41:12 194

原创 Go基础--18 协程机制(未完)

Thread vs Groutine1.创建默认的stack的大小JDK5以后java Thread stack 默认是1MGroutine的stack初始化大小为2Kpackage goroutineimport ( "fmt" "testing" "time")func TestGroutine(t *testing.T) { for i:=0 ; i<10;i++{ go func(){ //此时i共享地址,造成竞态,需要加锁 fmt.Printl

2021-05-27 22:24:59 66

原创 Go基础--16 构建可复用的包

package基本复用模块单元(以首字母大写来表明可以被外部调用)代码的package可以和所在的目录不一致同一目录里的Go代码的package要保持一致package clientimport ( "go-learn/src/class16/series" "testing")func TestPackage(t *testing.T){ t.Log(series.GetFibonacci(10))}init方法在main函数执行之前,所有依赖的package的i

2021-05-27 21:50:44 87

原创 Go基础--15 pannic与recover

panicpannic用于不可恢复得错误panic退出前会执行deferpanic vs os.Exitos.Exit退出时不会调用defer指定函数os.Exit退出时不输出当前调用栈信息package panic_recoverimport ( "errors" "fmt" "testing")func TestPanicVsExit(t *testing.T){ fmt.Println("start") defer func() { fmt.Println(

2021-05-27 21:25:18 503

原创 Go基础--14 编写好的错误处理

及早判断错误package errorimport ( "errors" "testing")var LessThanTwoError = errors.New("n should be not less than 2")var LargeThanError = errors.New("n should be not more than 100")func GetFibonacci(n int) ([]int,error){ if n < 2 { return nil,Less

2021-05-27 21:13:48 122

原创 Go基础--13 不一样的接口类型 一样的多态

多态参数是interface 只能是指针类型package polymorphicimport ( "fmt" "testing")type code stringtype Programmer interface { WriteHelloWolrd() code}type GoProgrammer struct {}type JavaProgrammer struct {}func (p *GoProgrammer) WriteHelloWolrd()

2021-05-26 23:36:21 59

原创 Go基础--12 扩展与复用

Go不支持继承类似继承,但是无法实现重载机制package extensionimport ( "fmt" "testing")type Pet struct {}func (p *Pet) Speack(){ fmt.Println("miao")}func (p *Pet) SpeakTo(){ p.Speack() fmt.Println("...")}type Dog struct { Pet}func (d *Dog) Speack(){

2021-05-26 22:46:23 91

原创 Go基础--11 相关接口

Go接口实现是不依赖Go接口的定义--duck type 类似就可以package _interfaceimport "testing"type code stringtype Programmer interface { WirteHelloWolrd() code}type GoProgrammer struct {}func (g *GoProgrammer) WirteHelloWolrd() code{ return "fmt.Println(\"Hello.

2021-05-26 22:26:41 68

原创 Go基础--10 行为定义和实现

定义结构体package encapsulationimport "testing"type Employee struct { Id string Age int64 Name string}func TestCreateEmployee(t *testing.T){ e := Employee{"0",20,"leo"} e1 := Employee{Name:"ioe"} e2 := new(Employee) //指针类型 e2.Id = "20" e2.Name

2021-05-26 21:57:23 88

原创 Go基础--09 函数

函数是一等公民可以有多个返回值 所有参数传递都是值传递:slice map channel 会有传引用错觉 函数可以作为变量的值 函数可以作为参数和返回值package _funcimport ( "fmt" "math/rand" "testing" "time")func ReturnMultiValues()(int,int){ return rand.Intn(10),rand.Intn(10)}func TimeSpent(inner func(op int)

2021-05-26 21:37:19 56

原创 Go基础--08 字符串

字符串string是数据类型,不是引用或指针类型 string是只读的byte slice,len函数可以它所包含的byte数 string的byte数组可以存放任何数据

2021-05-26 21:15:12 48

原创 数据结构--队列

创建队列#include <stdio.h>#include <stdlib.h>typedef struct Queue{ int head,tail,length; int *data;}Queue;void init(Queue *q,int length){ q->data = (int *)malloc(sizeof(int) * length); q->length = length; q->he

2021-05-26 08:20:12 90

原创 Go基础--07 map

map声明map声明后为空时候返回0package my_mapimport "testing"func TestInitMap(t *testing.T){ m1 := map[int]int{1:1,2:4,3:9} t.Log(m1[2]) t.Logf("len m1 %d",len(m1)) m2 := map[int]int{} m2[4] = 16 t.Logf("len m2=%d",len(m2)) m3 := make(map[int]int,10) //

2021-05-25 23:33:03 138

原创 Go基础--06 数组与切片

数组package arrayimport "testing"func TestArrayInit(t *testing.T){ var arr = [...]int{1,2,3} arr1 := [3]int{1,2} for i:=0;i<3;i++{ t.Log(arr[i]) } t.Log(arr1[1:])}func TestArrayLoop(t *testing.T){ arr3 := [...]int{1,2,3} for _,data := r

2021-05-25 23:07:16 77 1

原创 Go基础--05 循环与条件

循环Go语言仅支持循环关键字forpackage loopimport "testing"func TestWhileLoop(t *testing.T){ n := 0 for n < 5{ t.Log(n) n++ }}条件语句switch 不要加breakpackage conditionimport ( "runtime" "testing")func TestCondition(t *testing.T){ if a:=1 ==

2021-05-25 22:06:40 50

原创 Go基础-04 数组比较

数组比较package operator_testimport ( "testing")func TestCompareArray(t *testing.T){ a := [...]int{1,2,3} b := [...]int{1,2,3} c := [...]int{2,3,4} //相同位数才能比较 t.Log(a == b) t.Log(b == c)}

2021-05-25 21:41:38 513

原创 Go基础--03 类型

类型转化与其他编程语言差异Go语言不支持隐式类型转换 别名和原有类型也不能进行隐式类型转换package type_testimport "testing"type Myint intfunc TestImplict(t *testing.T){ var a int32 = 1 var b int64 b = int64(a) var c Myint a = int32(c) t.Log(a,b)}类型预定义的值math.MaxIn64 math.MaxFl

2021-05-25 21:33:56 51

原创 Go基础--02 变量

变量赋值赋值可以是进行自动类型推断 在一个赋值语句中可以对多个变量进行赋值斐波那契package fibimport ( "testing")func TestFibList(t *testing.T){ var ( a = 1 b = 1 ) t.Log(a) for i:=0 ; i < 5;i++{ t.Log(" ",b) tmp := a a = b b = tmp + a }}交换位置func TestExchang

2021-05-25 21:17:45 36

原创 Go基础--01 小试牛刀

Go语言必须以main函数作为入口main函数不支持传入参数,main函数不支持返回值package mainimport ( "fmt" "os")func main() { fmt.Println(os.Args) if len(os.Args) > 1{ fmt.Println("Hello wolrd",os.Args[1]) } fmt.Println("Hello Wolrd") os.Exit(1)}The master has failed

2021-05-23 22:58:47 44

原创 数据结构C语言版本--链表

元素相互依赖,串联而成(除了火车头,每节车厢只连前一节车厢)一个链表只有一个表头(火车只有一个火车头)元素不能随机访问创建链表头#include <stdio.h>#include <stdlib.h>typedef struct Node{ int data; struct Node *next;}Node,*LinkedList;void clear(LinkedList head){ Node *current_node =

2021-05-23 00:39:55 68

原创 Python分布式爬虫--url去重策略|unicode与url编码

url去重策略1.将访问过的url保存在数据库中2.将访问过的url保存在set中,只需要O(1)时间复杂度就可以查询到url,内存占用越来越大3.url讲过MD5编码缩减到一定长度字符存在set中(scrapy使用这种方法)4.bitmap,将访问过的url通过hash函数映射到某一位5.bloomfilter方法对于bitmap进行改进,多重hash函数降低冲突unicode与url编码字符串编码1.计算机只能处理数字,文本转换为数字才能处理,计算机8个bit作为一个字节

2021-05-16 22:29:28 179

原创 正则表达式

1.特殊字符^ 以xx开头$ 以xx结尾.匹配所有字符*前面字符出现任意多次?非贪婪模式 正则表达式是贪婪匹配,?是非贪婪模式import restr = "abcdabda" #匹配的字符 获取abcd字段re_str = re.match("^a.*a") print(re_str.group(1))#输出abda#正确的正则re_str = re.match("^a.*?a)print(re_str.group(1))#输出abcd+ 前面字符至少出现.

2021-05-15 23:29:35 78

原创 Python分布式爬虫--scrapy初识

安装虚拟环境--切换virtualenv -p 指定版本安装虚拟环境管理工具pip install virtualenvwrapper启动workonvirtualenvwrapper创建不同版本虚拟环境mkvirtualenv --python="其他版本路径" 名称scrapy框架scrapy框架基于twisted异步IO框架scrapy方便扩展,提供很多内置功能scrapy内置css和xpath selector非常方便,beautifulsoup最大

2021-05-15 16:29:54 64

原创 数据结构--顺序表

线性表线性表是由相同类型数据元素组成的有限序列 线性表受存储空间限制,线性表不能无限存储 从逻辑上看,线性表的元素按顺序依次排列顺序表顺序表是线性表的一种顺序存储形式,换句话说线性表是逻辑结构,顺序表是存储结构,是指用一组连续的存储单元,依次存储线性表中的数据元素,从而逻辑上相邻的数据元素物理位置上也相邻。顺序表通常用一维数组来实现,一维数组可以是静态分配也可以是动态分配。顺序表最主要是可以随机访问,时间复杂度O(1)顺序表不足之处插入和删除需要移动大量元素,保持逻辑和物理上的连续

2021-05-14 08:37:04 1361

原创 Jenkins Linux shell集成和Jenkins参数集成

创建freeStyle风格的Jenkins创建shell脚本#!/bin/shuser=`whoami`if [ $user == 'deploy' ]then echo "Hello,My name is $user"else echo "Sorry,I am not user"fiip addrcat /etc/system-releasefree -mdf -hpy_cmd=`which python`$py_cmd --version

2021-05-12 21:26:37 122

原创 Jenkins安装

安装参考链接http://pkg.jenkins-ci.org/redhat/安装Javayum install javajava -version关闭防火墙getenforceJenkins安装与初始化yum install jenkinsvim /etc/sysconfig/jenkinsJENKINS_USER="deploy"JENKINS_PORT="8080"切换用户su deploychown -R deploy:deploy /.

2021-05-11 22:22:42 59

原创 Ansible playbooks常用模块

创建nginx目录mkdir /etc/nginx安装nginx rpmrpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpmansible-playbook -i inventory/testenv ./deply.yml 验证ansible是否执行成功ssh root@cababage.vip ls -l /root/foo.t

2021-05-10 22:27:11 94

原创 Ansible常用模块

File模块在目标主机创建文件或目录,并且赋予其系统权限- name:create a file file: 'path=/root/foo.txt state=touch mode=0755 owner=foo group=foo'Copy模块实现Ansible服务端到目标主机文件传送- name:copy a file copy: 'remote_src=no src=roles/testbox/files/foo.sh dest=/root/foo.sh mode=06

2021-05-09 00:09:43 69

原创 Ansible playboooks入门和编写规范

Test Playbooksinventory/ Server详细清单目录 保存目标主机的相关域名和目标地址 testenv 具体清单与变量声明文件roles/ roles任务列表 testbox/ testbox详细任务 tasks/ main.yml testbox主任务文件deploy.yml Plakbook任务入口文件testenv#详细目录testenv[testservers] #s...

2021-05-08 23:39:29 82

空空如也

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除