Download

首先确定操作系统位数, 可执行:

1
$ uname -m

如果输出为 x86_64 的话代表64位操作系统,如果为 i386 则为32为系统。

根据确定的系统位数下载对应版本的安装包,我的是系统是64,所以

1
$ wget https://storage.googleapis.com/golang/go1.6.2.linux-amd64.tar.gz

解压到安装目录, 我把go安装到 /usr/local/go 中,执行:

1
2
$ tar zxvf go1.6.2.linux-amd64.tar.gz
$ mv go /usr/local

配置环境变量

添加 GOROOT 环境变量

1
$ export GOROOT="/usr/local/go"

可以通过执行 env 或者 echo $GOROOT 来确认环境变量是否正常设置.

设置PATH

1
$ export PATH=$PATH:/usr/local/go/bin

测试是否安装成功

1
$ go version

输出 go version go1.6.2 linux/amd64 表示golang已经成功安装!

GOPATH

剩下一步比较重要的就是设置 GOPATH, 详情可查看资料

例如我将GOPATH设置为 /home/go 为我的工作空间:

1
$ export GOPATH="/home/go"

/home/go的目录结构为:

1
2
3
4
-- go
--bin
--pkg
--src

有关目录结构的介绍可以参考:https://github.com/astaxie/build-web-application-with-golang/blob/master/zh/01.2.md

至此Golang已经成功安装!

小知识

直接在命令行运行 $ export PATH=$PATH:/usr/local/go, 只会在当前会话有效,登出或者注销系统后,PATH的设置就会失效。

如果想永久保存设置,则需要修改/etc/profile,在末尾加上(如果之前没有添加过的话):

1
2
3
4
PATH=$PATH:/usr/local/go
export PATH
GOPATH=/home/go
export GOPATH

然后执行

1
$source /etc/profile

之后可以通过 echo $PATH 来查看设置是否生效。

首先从一个现实的需求中说起,在接口业务中有多个版本,突然在开发第三个版本的
时候其中有一条需求就是把用户提交的评分值由原来的允许的1, 2, 3, 4, 5 改成允许输入 0, 2, 4, 6, 8, 10

自定义的validator:

1
2
3
4
5
6
7
8
9
10
11
12
//ScoreValidator.php
class ScoreValidator extends Validator
{
protected function validateValue($value)
{
if (in_array($value, [1, 2, 3, 4, 5])) {
return null;
}

return [$this->message, []];
}
}
1
2
3
4
5
6
7
8
9
//User.php
public function rules()
{
return [
...
['rate', ScoreValidator::className()],
...
];
}

那么应该如何做兼容性,来保证多个版本的接口中都按照规则来赋值呢?我第一个想到的是利用di来解决。

为什么这么想?可以参考下之前的一篇文章

那么我们来具体实施.

首先,接口版本控制的结构是遵循官方来的:

1
2
3
4
5
6
7
8
9
modules
--v1
-- controllers
-- ...
-- Module.php
--v2
-- controllers
-- ...
-- Module.php

在这之前我们需要对之前定义的ScoreValidator进行下修改:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//ScoreValidator.php
class ScoreValidator extends Validator
{
public $ranges = [0, 2, 4, 6, 8, 10];// 这里直接改成满足现有需求的数据区间

protected function validateValue($value)
{
if (in_array($value, $this->ranges)) {
return null;
}

return [$this->message, []];
}
}

我们希望在v2版本的接口中来实现默认值的修改, 那么可以在原来的v1版本下的Module::init中来配置:

1
2
3
4
5
6
7
8
9
10
11
class Module extend \yii\base\Module
{
public function init()
{
parent::init();

Yii::$container->set(ScoreValidator::className(), [
'ranges' => [1, 2, 3, 4, 5]
]);
}
}

这样在v1接口下的涉及到ScoreValidator验证的地方就会按照这个规则来进行验证了。同样新的需求也可以兼顾到。

利用Yii2的容器和依赖注入可以很轻松写意的实现更多,还需要我们不停的探索!

git tip one

git config

git config 配置项由本地 > 用户 > 系统层级覆盖。

所以在本地某个仓库可以设置用户名和邮箱:

git config user.name “light”
git config user.email “light@qq.com

为常用命令设置快捷别名

git config alias.st status
git config alias.ci commit

同样可以使用-g 来进行全局配置

使用:

$ git st
$ git ci

开启xdebug

php.ini中配置xdebug如下:

[xdebug]
xdebug.remote_enable = On
xdebug.remote_handler = dbgp
xdebug.remote_host= localhost
xdebug.remote_port = 9000
xdebug.idekey = PHPSTORM
xdebug.profiler_enable = 1

重启服务之后在phpinfo中查看是否已经开启xdebug.

配置phpstorm

  1. 配置xdebug端口号

    在 File > Settings > Langauges&Frameworks 中找到 PHP 选择Debug选项卡,找到xdebug配置项,确认端口号是上面设置的9000

  2. 配置DBGp Proxy

    点击DBGp Proxy选项卡,在里面设置:

    IDE Key: PHPSTORM
    Host: localhost
    Port: 9000

  3. 添加 servers

    找到 Servers选项卡,点击添加按钮添加一个配置信息。例如想调试部署好的虚拟地址:www.app.local,那么你需要在Host中填上该地址。
    注意Port需要和你实际配置的端口要一直。另外保证DebuggerXdebug

这里主要记录下如何配合浏览器插件Xdebug helper来进行调试

安装浏览器插件Xdebug helper

安装成功之后,在选线卡将你配置的domain进行添加。

在postman中使用xdebug

  1. Configure your xdebug (by editing php.ini) to attempt to debug every php script (xdebug.remote_autostart = 1)

  2. Add xdebug session start parameter to the actual URL (XDEBUG_SESSION_START= – http://xdebug.org/docs/remote ), for example: ?XDEBUG_SESSION_START=PHPSTORM

  3. Pass xdebug cookie as one of the headers (the one which is set by bookmarklet or browser extension, for example)

    参考文章:

首先在travis将你的项目加入到travis。

打开设置,添加token变量。

add-token

在这之前我们需要在github, 生成一个token。

generate-token

在项目的 .travis.yml 中可以进行如下配置:

language: node_js
sudo: false
node_js:
  - 5.3.0

before_install:
  - git config --global user.name "lichunqiang"
  - git config --global user.email "light-li@hotmail.com"
  - git config --global push.default simple

install:
  - npm install hexo-cli -g

script:
  - git clone https://${token}@github.com/lichunqiang/lichunqiang.github.io.git .deploy_git -v
  - git --version
  - git remote add -f b https://${token}@github.com/lichunqiang/b.git
  - git fetch b
  - npm install

  - hexo g
  - cp -r public/* .deploy_git/
  - cd .deploy_git
  - git add -A
  - git commit -am "Auto deploy from Travis-CI."
  - git push --force -q

这样就等着 travis 构建完成之后,我们就可以看到文件就会自动发布了。

  1. Delete the relevant section from the .gitmodules file.

  2. Stage the .gitmodules changes git add .gitmodules

  3. Delete the relevant section from .git/config.

  4. Run git rm –cached path_to_submodule (no trailing slash).

  5. Run rm -rf .git/modules/path_to_submodule

  6. Commit git commit -m “Removed submodule

  7. Delete the now untracked submodule files rm -rf path_to_submodule

www.site.com site.com 在配置nginx的时候都会指向我们的主站,但是需要注意的是在这里要进行一下跳转设置,

因为在不设置的话,在百度等收录的话就会当做成两个站点,就行重复收录。

配置nginx如下:

server_name site.com www.site.com

if ($host = ‘site.com’) {
rewrite ^/(.*)$ http://www.site.com/$1 permanent;
}

Github 上进行协作或者贡献开源项目代码时, 我们常常会对项目发起 pull request

在项目的作者对发起的 pull request 进行审核时, 常常需要我们对修改进行进一步的修订, 以满足作者的需求。方便的是,

这一切在 Github 上被方便的给自动完成了,也就说,当我们在对 patch 分支 push 修改代码时,

Github 自动会将修改关联到上次已经发起的 pull request

当这一切都ready之后,我们需要精简提交信息变成一条,例如: Issue #100: bugfix for fatal error.

commit 6e9cd88424b5f313f7245c7a43e11063648f2dcd
Author: lichunqiang <light-li@hotmail.com>
Date:   Mon Jan 11 16:40:41 2016 +0800

    tweak the description

commit 06ce7e35206b0f8a2e5d41aab4ae21d842a7c8ab
Author: lichunqiang <light-li@hotmail.com>
Date:   Sun Jan 10 23:04:25 2016 +0800

    bugfix of mailer

commit e6e0ee8ec14624d11035a0a922617ab5dc69220f
Author: lichunqiang <light-li@hotmail.com>
Date:   Sun Jan 10 22:57:25 2016 +0800

    commit message

在 git log 信息中,我们想要压缩最新的两条commit为一条

我们可以按照以下的步骤来进行:

$ git rebase -i HEAD~2

2 代表我们想要压缩的commit数量。注意,在这之前,我们要保证我们的分支要和主线进行了同步。

执行命令后,在 text editor 中我们将 pick 替换成 squash, 只保留我们想保存的那条 commit 即可。

也可以只保留第一条的 pick, 将其余的替换成 squash。保存并退出编辑器。

注意,如果你已经提交到远程仓库,你需要执行:

$ git push origin branchname --force

贴士:

你可以执行 git commit --amend 来修改最后一条commit的提交信息。

我们通常会混迹多个git平台, 这时我们需要配置能够在多个平台通用的ssh.

设置用户名和邮箱

$git config --global user.name "yourname"
$git config --global user.email "your@example.com"

生成ssh-key

ssh-key保存在 ~/.ssh 目录中

$cd ~/.ssh
$ssh-keygen -t rsa -C "your@example.com"

注意在生成key file的时候可以针对不同的平台来命名, 例如:id_github, 这将会在接下的来的配置中用到.

生成Key之后就是将 xx.pub 的内容配置到平台的SSH配置中.

配置文件

~/.ssh 目录下新建 config 文件并编辑:

Host git.coding.net
    User your@example.com
    IdentityFile ~/.ssh/coding_rsa
Host github.com
    User your@example.com
    IdentityFile ~/.ssh/github_rsa

测试是否连接成功

$ssh -T git@git.coding.net
$ssh -T git@github.com