工作,学习,生活,这里将会有一些记录. 备用域名:http://meisw.wdlinux.cn 注册 | 登陆
浏览模式: 标准 | 列表全部文章

Golang给目录按时间排序.正则、时间条件搜索文件

 http://blog.csdn.net/fyxichen/article/details/50991805

golang时间函数处理

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 如何获取三个月前的月份日期

  1. func main () {
  2. now := time.Now()
  3.  
  4. yesterday := now.AddDate( 0, -1, 0 )
  5. bef_yes := yesterday.AddDate ( 0, -1, 0 )
  6.  
  7. fmt.Printf("Today:%s\n", now.Format("200601"))
  8. fmt.Printf("Yesterday:%s\n", yesterday.Format("200601"))
  9. 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
--------------
golang time 时间的加减法
http://blog.csdn.net/codyguo/article/details/53009451
 

使用golang生成证书

 http://studygolang.com/articles/9959

 
http://blog.csdn.net/yevvzi/article/details/54346519

ssl and acme_tiny.py

 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

以太坊生态系统中的工具和技术

 http://ethfans.org/posts/tools-and-technologies-in-the-ethereum-ecosystem

golang null值处理

NULL值处理

简单说就是设计数据库的时候不要出现null,处理起来非常费力。Null的type很有限,例如没有sql.NullUint64; null值没有默认零值。

for rows.Next() {     var s sql.NullString     err := rows.Scan(&s)     // check err     if s.Valid {        // use s.String     } else {        // NULL value     } }

 

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

squid 配置参数

 #./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限制文件下载或访问

 禁止终端用户在任何客户机上下载文件扩展名为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