Submitted by admin on 2017, June 11, 1:17 AM
golang时间处理
相关包 "time"
-
时间戳
当前时间戳
fmt.Println(time.Now().Unix()) # 1389058332
-
str格式化时间
当前格式化时间
fmt.Println(time.Now().Format("2006-01-02 15:04:05")) # 这是个奇葩,必须是这个时间点, 据说是go诞生之日, 记忆方法:6-1-2-3-4-5 # 2014-01-07 09:42:20
-
时间戳转str格式化时间
str_time := time.Unix(1389058332, 0).Format("2006-01-02 15:04:05") fmt.Println(str_time) # 2014-01-07 09:32:12
-
str格式化时间转时间戳
这个比较麻烦
the_time := time.Date(2014, 1, 7, 5, 50, 4, 0, time.Local) unix_time := the_time.Unix() fmt.Println(unix_time) # 389045004
还有一种方法,使用time.Parse
the_time, err := time.Parse("2006-01-02 15:04:05", "2014-01-08 09:04:41") if err == nil { unix_time := the_time.Unix() fmt.Println(unix_time) } # 1389171881
http://studygolang.com/articles/669
--------------------
Go 如何获取三个月前的月份日期
func main () {
now := time.Now()
-
yesterday := now.AddDate( 0, -1, 0 )
bef_yes := yesterday.AddDate ( 0, -1, 0 )
-
fmt.Printf("Today:%s\n", now.Format("200601"))
fmt.Printf("Yesterday:%s\n", yesterday.Format("200601"))
fmt.Printf("Yesterdat before Yesterday:%s\n",bef_yes.Format("200601"))
http://www.golangtc.com/t/51bd288c320b5264b800001c
--------------
时间比较
先把当前时间格式化成相同格式的字符串,然后使用time的Before, After, Equal 方法即可.
|
1
2
3
4
5
6
7
8
9
|
time1 := "2015-03-20 08:50:29"
time2 := "2015-03-21 09:04:25"
t1, err := time.Parse("2006-01-02 15:04:05", time1)
t2, err := time.Parse("2006-01-02 15:04:05", time2)
if err == nil && t1.Before(t2) {
fmt.Println("true")
}
|
http://www.jb51.net/article/64705.htm
--------------
http://blog.csdn.net/codyguo/article/details/53009451
golang | 评论:0
| Trackbacks:0
| 阅读:1109
Submitted by admin on 2017, June 8, 2:48 PM
http://studygolang.com/articles/9959
http://blog.csdn.net/yevvzi/article/details/54346519
golang | 评论:0
| Trackbacks:0
| 阅读:893
Submitted by admin on 2017, June 8, 10:37 AM
openssl genrsa 4096 > account.key
wget http://dl.wdlinux.cn/files/openssl/openssl-1.0.2k.tar.gz
tar zxvf openssl-1.0.2k.tar.gz
cd openssl-1.0.2k
./config --prefix=/usr/local/openssl102k
make
mkdir /etc/ssl
cp apps/openssl.cnf /etc/ssl/
cd ..
openssl req -new -sha256 -key domain.key -subj "/" -reqexts SAN -config <(cat /etc/ssl/openssl.cnf <(printf "[SAN]\nsubjectAltName=DNS:51099.com,DNS:www.51099.com")) > domain.csr
mkdir /www/web/challenges
vi /www/wdlinux/nginx/conf/vhost/51099.com.conf
service nginxd restart
python -v
wget https://raw.githubusercontent.com/diafygi/acme-tiny/master/acme_tiny.py --no-check-certificate
python acme_tiny.py --account-key ./account.key --csr ./domain.csr --acme-dir /www/web/challenges/ > ./signed.crt
pip
yum install python-pip
wget "https://pypi.python.org/packages/11/b6/abcb525026a4be042b486df43905d6893fb04f05aac21c32c638e939e447/pip-9.0.1.tar.gz#md5=35f01da33009719497f01a4ba69d63c9" --no-check-certificate
tar zxvf pip-9.0.1.tar.gz
cd pip-9.0.1
python setup.py install
cd ..
python acme_tiny.py --account-key ./account.key --csr ./domain.csr --acme-dir /www/web/challenges/ > ./signed.crt
pip install argparse
python acme_tiny.py --account-key ./account.key --csr ./domain.csr --acme-dir /www/web/challenges/ > ./signed.crt
wget -O - https://letsencrypt.org/certs/lets-encrypt-x3-cross-signed.pem > intermediate.pem
cat signed.crt intermediate.pem > chained.pem
wget -O - https://letsencrypt.org/certs/isrgrootx1.pem > root.pem
cd ..
ls /www/
mv ssl /www/
cd /www/
cd ssl/
cp chained.pem domain.key /www/wdlinux/nginx/conf/cer/
openssl req -new -sha256 -key domain.key -subj "/" -reqexts SAN -config <(cat /etc/ssl/openssl.cnf <(printf "[SAN]\nsubjectAltName=DNS:baidu.com.com,DNS:www.baidu.com")) > baidu.csr
python acme_tiny.py --account-key ./account.key --csr ./baidu.csr --acme-dir /www/web/challenges/ > ./baidu.crt
vim acme_tiny.py
vi /bin/renew_cert.sh
#!/bin/bash
cd /www/ssl/
python acme_tiny.py --account-key account.key --csr domain.csr --acme-dir /www/web/challenges/ > signed.crt || exit
wget -O - https://letsencrypt.org/certs/lets-encrypt-x3-cross-signed.pem > intermediate.pem
cat signed.crt intermediate.pem > chained.pem
service nginx reload
chmod 700 /bin/renew_cert.sh
#0 0 1 * * root /bin/renew_cert.sh
apache/web | 评论:0
| Trackbacks:0
| 阅读:950
Submitted by admin on 2017, June 6, 10:09 PM
http://ethfans.org/posts/tools-and-technologies-in-the-ethereum-ecosystem
区块链 | 评论:0
| Trackbacks:0
| 阅读:979
Submitted by admin on 2017, June 4, 1:40 AM
NULL值处理
简单说就是设计数据库的时候不要出现null,处理起来非常费力。Null的type很有限,例如没有sql.NullUint64; null值没有默认零值。
for rows.Next() { var s sql.NullString err := rows.Scan(&s) if s.Valid { } else { } }
https://segmentfault.com/a/1190000003036452
------------
而对于在Go中构建页面, text/template 中的很多功能不会自动判断Nullable类型,比如 {{if .Property}} ,如果 Property 属性是Nullable类型,且他是 NULL 即 Valid 属性为 false ,但是模板中的 if 还是会认为 true ,因为这个Nullable类型本身是一个值所以 if 会认为是 true,而Nullable类型本身到底是不是 NULL 根本没有意义,所以必须要写 {{if .Property.Valid}}。在输出上,也要写 {{.Property.String}} (不同Nullable类型值得属性会不一样,这里以 sql.NullString 演示)。
或者用实际代码演示这个问题:
package main import ( "bytes" "database/sql" "fmt" "text/template" ) type Test struct { EmptyString sql.NullString NonEmptyString sql.NullString } func main() { test := &Test{} test.EmptyString = sql.NullString{Valid: false} test.NonEmptyString = sql.NullString{Valid: true, String: "Mgen"} template := template.Must(template.New("test").Parse("{{if .EmptyString}}{{.NonEmptyString}}{{end}}")) buffer := &bytes.Buffer{} err := template.Execute(buffer, test) if err != nil { panic(err) } fmt.Print(buffer.String()) }
这段代码会输出:
{Mgen true}
验证了上面讲的两个问题:
- 为
NULL 的Nullable类型会在模板 if 中直接理解成 true .
- 输出问题,上面输出
{Mgen true} 实际上就是把 sql.NullString 的两个内部属性全部输出出来了。
Go中 NullString 类型定义:
type NullString struct { String string Valid bool // Valid is true if String is not NULL }
所以正确的模板应该这样写:
template := template.Must(template.New("test").Parse("{{if .EmptyString.Valid}}{{.NonEmptyString.String}}{{end}}"))
总之一旦遇到数据库中的 NULL ,还是会稍微有些麻烦的,目前的解决方案可供选择:
- 数据库中尽量不存
NULL 值,或者使用 ISNULL 或 COALESCE 对 NULL 值坐下处理。
- JSON上对Nullable类型进行改造,模板定义上属性要针对Nullable类型的属性做判断。
- 不需要Nullable,
NULL 值转换成空值, 这是文章开头我说的愿望:joy:,目前不支持 。
http://www.tuicool.com/articles/QJZJjaQ
golang | 评论:0
| Trackbacks:0
| 阅读:1000
Submitted by admin on 2017, May 25, 12:19 PM
#./configure --prefix=/usr/local/squid3 //指定软件的安装路径
--disable-internal-dns //使用自己内部DNS查询
--disable-ident-lookups //防止系统使用RFC931规定的身份识别方
--disable-carp //Cache数组路由协议(CARP)用来转发丢失的cache到父cache的数组或cluste
--disable-wccp //用于阻止或分发HTTP请求到一个或多个caches
--enable-gnuregex //支持GNU正则表达式
--enable-async-io=240 //等同于同时开启./configure如下三个选项:
--with-aufs-threads=N_THREADS
--with-pthreads
--enable-storeio=ufs,aufs
这个主要是设置async模式来运行squid,我的理解是设置用线程来运行squid,如果服务器配置很不错,有1G以上内存,cpu使用SMP的方式的话可以考虑设成160或者更高。
如果服务器比较糟糕就根据实际情况设了。另外此项还另cache文件支持aufs
--enable-icmp //加入ICMP支持
--enable-kill-parent-hack //关掉squid的时候,要不要连同父进程一起关掉
--with-maxfd=65535 //指定最大文件描述
--with-dl //
--with-large-files //
--enable-poll //指定使用Poll()函数,提升性能就是啦
--enable-linux-netfilter //可以支持透明代理
--enable-large-cache-files //开启大文件支持,支持2GB以上的文件
--enable-delay-pools //开启squid延时池功能
--enable-snmp //此选项可以让MRTG使用SNMP协议对服务器的流量状态进行监测,因此必须选择此项,使Squid支持SNMP接口
--enable-underscore //允许解析的URL中出现下划线,因为默认squid会认为带下划线的URL地址是非法的,并拒绝访问该地址
--enable-arp-acl //可以在规则设置中直接通过客户端的MAC地址进行管理,防止客户使用IP欺骗
--enable-cache-digests //加快请求时,检索缓存内容的速度
--enable-err-language=”Simplify_Chinese” //
--enable-default-err-languages=”Simplify_Chinese” //指定出错是显示的错误页面为简体中文
squid/缓存 | 评论:0
| Trackbacks:0
| 阅读:1049
Submitted by admin on 2017, May 25, 12:13 PM
禁止终端用户在任何客户机上下载文件扩展名为mp3、exe、zip和rar类型的文件
acl badfile urlpath_regex –i .mp3$ .exe$ .zip$ .rar$ .rmvb$ .rm$ .mp4$ ……
http_access deny badfile
禁止客户机IP地址在192.168.2.0子网的所有终端客户在星期一到星期五的9:00到18:00访问Internet资源
acl clientnet src 192.168.2.0/24
acl worktime time MTWHF 9:00-18:00
http_access deny clientnet worktime
限制IP地址为192.168.1.102的客户机并发连接的最大连接数为5
acl clientip src 192.168.1.102
acl clientmaxconn maxconn 5
http_access deny clientip clientmaxconn
禁止所有终端用户访问域名包含为google.com的网站
acl badurl url_regex –i google.com
http_access deny badurl
禁止所有终端用户访问域名为www.google.com的网站
acl baddomain dstdomain –i www.google.com
http_access deny baddomain
squid/缓存 | 评论:0
| Trackbacks:0
| 阅读:997
Submitted by admin on 2017, May 22, 10:55 PM
主代码: package rsa import ( "crypto/rand" "crypto/rsa" "crypto/x509" "crypto/x509/pkix" "encoding/pem" "io/ioutil" "math/big" rd "math/rand" "os" "time" ) func init() { rd.Seed(time.Now().UnixNano()) } type CertInformation struct { Country []string Organization []string OrganizationalUnit []string EmailAddress []string Province []string Locality []string CommonName string CrtName, KeyName string IsCA bool Names []pkix.AttributeTypeAndValue } func CreateCRT(RootCa *x509.Certificate, RootKey *rsa.PrivateKey, info CertInformation) error { Crt := newCertificate(info) Key, err := rsa.GenerateKey(rand.Reader, 2048) if err != nil { return err } var buf []byte if RootCa == nil || RootKey == nil { buf, err = x509.CreateCertificate(rand.Reader, Crt, Crt, &Key.PublicKey, Key) } else { buf, err = x509.CreateCertificate(rand.Reader, Crt, RootCa, &Key.PublicKey, RootKey) } if err != nil { return err } err = write(info.CrtName, "CERTIFICATE", buf) if err != nil { return err } buf = x509.MarshalPKCS1PrivateKey(Key) return write(info.KeyName, "PRIVATE KEY", buf) } func write(filename, Type string, p []byte) error { File, err := os.Create(filename) defer File.Close() if err != nil { return err } var b *pem.Block = &pem.Block{Bytes: p, Type: Type} return pem.Encode(File, b) } func Parse(crtPath, keyPath string) (rootcertificate *x509.Certificate, rootPrivateKey *rsa.PrivateKey, err error) { rootcertificate, err = ParseCrt(crtPath) if err != nil { return } rootPrivateKey, err = ParseKey(keyPath) return } func ParseCrt(path string) (*x509.Certificate, error) { buf, err := ioutil.ReadFile(path) if err != nil { return nil, err } p := &pem.Block{} p, buf = pem.Decode(buf) return x509.ParseCertificate(p.Bytes) } func ParseKey(path string) (*rsa.PrivateKey, error) { buf, err := ioutil.ReadFile(path) if err != nil { return nil, err } p, buf := pem.Decode(buf) return x509.ParsePKCS1PrivateKey(p.Bytes) } func newCertificate(info CertInformation) *x509.Certificate { return &x509.Certificate{ SerialNumber: big.NewInt(rd.Int63()), Subject: pkix.Name{ Country: info.Country, Organization: info.Organization, OrganizationalUnit: info.OrganizationalUnit, Province: info.Province, CommonName: info.CommonName, Locality: info.Locality, ExtraNames: info.Names, }, NotBefore: time.Now(), NotAfter: time.Now().AddDate(20, 0, 0), BasicConstraintsValid: true, IsCA: info.IsCA, ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth, x509.ExtKeyUsageServerAuth}, KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign, EmailAddresses: info.EmailAddress, } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
测试代码: package rsa import ( "crypto/x509/pkix" "encoding/asn1" "os" "testing" ) func Test_crt(t *testing.T) { baseinfo := CertInformation{Country: []string{"CN"}, Organization: []string{"WS"}, IsCA: true, OrganizationalUnit: []string{"work-stacks"}, EmailAddress: []string{"czxichen@163.com"}, Locality: []string{"SuZhou"}, Province: []string{"JiangSu"}, CommonName: "Work-Stacks", CrtName: "test_root.crt", KeyName: "test_root.key"} err := CreateCRT(nil, nil, baseinfo) if err != nil { t.Log("Create crt error,Error info:", err) return } crtinfo := baseinfo crtinfo.IsCA = false crtinfo.CrtName = "test_server.crt" crtinfo.KeyName = "test_server.key" crtinfo.Names = []pkix.AttributeTypeAndValue{{asn1.ObjectIdentifier{2, 1, 3}, "MAC_ADDR"}} //添加扩展字段用来做自定义使用 crt, pri, err := Parse(baseinfo.CrtName, baseinfo.KeyName) if err != nil { t.Log("Parse crt error,Error info:", err) return } err = CreateCRT(crt, pri, crtinfo) if err != nil { t.Log("Create crt error,Error info:", err) } os.Remove(baseinfo.CrtName) os.Remove(baseinfo.KeyName) os.Remove(crtinfo.CrtName) os.Remove(crtinfo.KeyName) }
http://blog.csdn.net/fyxichen/article/details/53010255
golang | 评论:0
| Trackbacks:0
| 阅读:974