2020年12月24日 星期四

golang心得

環境

作業系統:Windows 10
golang版本:go version go1.15.6 windows/amd64
編輯器:GoLand 2019.3.4

Go 命令行

windows 10可直接在 git bash執行

檢查版本

$ go version
go version go1.15.6 windows/amd64

檢查GOPATH

$ echo $GOPATH
C:\Users\user\go

build命令

http://c.biancheng.net/view/120.html  go build命令(go语言编译命令)完全攻略
$ ls
hello.go
$ go build hello.go
$ ls
hello.exe*  hello.go
$ ./hello.exe
Hello, 世界
30




GoLand

熱鍵(使用sublime基礎的Keymap)

走到下一個斷點:(Resume Program) => F9  (需自定義)
快速複製檔案名字 :ctrl+shift+p => Rename File 


第一個golang程式

hello.go

package main

import "fmt"

func main() {
fmt.Println("Hello, 世界")
fmt.Println(test(3))
}

func test(n int) int {
return n * 10
}

使用GoLand斷點

https://studygolang.com/articles/20746  使用 GoLand 进行调试的要点
https://opensource.com/article/20/6/debug-go-delve  Stop debugging Go with Println and use Delve instead - 純CLI調試

ctrl + shift + p => Debug
go build hello.go => Debug
然後就會自動執行,停在你之前設定斷點的地方(或是直接 Debug 'go build hello.go'

計算表達式

ctrl+shift+p => Evaluate Expression
但是不支持調用函數,如這邊直接調用test(4)方法(test(4)=4*10=40)
報錯
could not recover call injection state for goroutine x

原因

https://github.com/go-delve/delve/issues/1710#issuecomment-604395056  Version of Delve is too old for this version of Go (maximum supported version 1.12, suppress this error with --check-go-version=false)


GoLand 2019.3.4 內置的 Delve Debugger 版本(1.4.0)太舊
位置:C:\Program Files\JetBrains\GoLand 2019.3.4\plugins\go\lib\dlv\windows\dlv.exe

解法1

升級GoLand

解法2 

手動安裝最新版dlv
$ go get -u github.com/go-delve/delve/cmd/dlv
檢查dlv版本
$ dlv version
Delve Debugger
Version: 1.5.1
Build: $Id: bca418ea7ae2a4dcda985e623625da727d4525b5 $

檢查是否安裝dlv
$ go list ... | grep dlv
github.com/go-delve/delve/cmd/dlv
github.com/go-delve/delve/cmd/dlv/cmds

查dlv位置
$ which dlv
/c/Users/user/go/bin/dlv

把 C:\Users\user\go\bin\dlv.exe 複製到 C:\Program Files\JetBrains\GoLand 2019.3.4\plugins\go\lib\dlv\windows\  下(記得把GoLand 原本舊版本的dlv先備份)

然後就可以在斷點時成功調用函數

手動命令行安裝完dlv後,會安裝到 C:\Users\user\go 下(有bin和src目錄)。GoLand就會自動抓到GOPATH



總結

GoLand的dlv斷點配置起來比PHPstorm的XDebug 簡單,但是用起來沒有那麼順手。





2020年12月15日 星期二

同時跑兩個PHP版本(5.6+7.3)的laradock

 參考資料

實作

拉兩個目錄的laradock,到時候跑同一個目錄的 info.php ( php_info(); ),相同的 nginx/sites/ 配置
/var/www/html/laradock/ - 跑PHP 7.3
/var/www/html/laradock5/ - 跑PHP 5.6

/var/www/html/laradock5/.env

使用不同的project name,最後會build出的容器名字會是:laradock5_docker-in-docker_1、laradock5_nginx_1、laradock5_php-fpm_1、laradock5_workspace_1
#   COMPOSE_PROJECT_NAME=laradock
COMPOSE_PROJECT_NAME=laradock5

使用PHP 5.6
#   PHP_VERSION=7.3
PHP_VERSION=5.6

如果你的網路不好,一直裝不成node,可以這樣跳過安裝node
#   WORKSPACE_INSTALL_NODE=true
WORKSPACE_INSTALL_NODE=false

如果你的網路不好,一直裝不成yarn,可以這樣跳過安裝yarn
#   WORKSPACE_INSTALL_YARN=true
WORKSPACE_INSTALL_YARN=false

避免端口和 /var/www/html/laradock/ (PHP 7.3 )的容器衝突
#   WORKSPACE_SSH_PORT=2222
WORKSPACE_SSH_PORT=2226
...
#   WORKSPACE_BROWSERSYNC_HOST_PORT=3000
WORKSPACE_BROWSERSYNC_HOST_PORT=3004
#   WORKSPACE_BROWSERSYNC_UI_HOST_PORT=3001
WORKSPACE_BROWSERSYNC_UI_HOST_PORT=3005
#   WORKSPACE_VUE_CLI_SERVE_HOST_PORT=8080
WORKSPACE_VUE_CLI_SERVE_HOST_PORT=8084
#   WORKSPACE_VUE_CLI_UI_HOST_PORT=8001
WORKSPACE_VUE_CLI_UI_HOST_PORT=8005

使用5567端口訪問PHP 5.6容器(PHP 7.3 端口5566)
#   NGINX_HOST_HTTP_PORT=80
NGINX_HOST_HTTP_PORT=5567
#   NGINX_HOST_HTTPS_PORT=443
NGINX_HOST_HTTPS_PORT=444

#   VARNISH_BACKEND_PORT=81
VARNISH_BACKEND_PORT=82

/var/www/html/laradock/ 和 /var/www/html/laradock5/ 都執行
[user@localhost laradock5 ]$ docker-compose up -d nginx workspace
Starting laradock5_docker-in-docker_1 ... done
Starting laradock5_workspace_1        ... done
Starting laradock5_php-fpm_1          ... done
Starting laradock5_nginx_1            ... done

[user@localhost laradock ]$ docker-compose up -d nginx workspace
Starting laradock_docker-in-docker_1 ... done
Recreating laradock_workspace_1      ... done
Recreating laradock_php-fpm_1        ... done
Recreating laradock_nginx_1          ... done

檢查

打開 http://app3.test:5566/info.php (php 7.3) 和 http://app3.test:5567/info.php (php 5.6) 






2020年12月14日 星期一

PHP 使用‌‌preg_match()的named subpattern

參考資料


Example #4 Using named subpattern

$str = 'foobar: 2008';

preg_match('/(?P<name>\w+): (?P<digit>\d+)/', $str, $matches);

/* 下面例子在php 5.2.2(pcre 7.0)或更新版本下工作, 然而, 为了后向兼容, 上面的方式是推荐写法. */
// preg_match('/(?<name>\w+): (?<digit>\d+)/', $str, $matches);

print_r($matches);


s

輸出

Array
(
    [0] => foobar: 2008
    [name] => foobar
    [1] => foobar
    [digit] => 2008
    [2] => 2008
)

使用 (?P<name>\w+) 可讓  preg_match() 的 $matches 結果帶有指定的key






2020年12月4日 星期五

windows 10 docker心得

前言

到了2020年才知道docker也在windows 能執行了,之前一直都是在hyper-v的虛擬機上面跑
https://docs.docker.com/docker-for-windows/release-notes/  Docker Desktop for Windows Stable Release notes

安裝

https://docs.docker.com/docker-for-windows/install/  Install Docker Desktop on Windows
安裝成功後開機會自動執行Docker Desktop


開始使用

安裝成功Docker Desktop後無containers和images的畫面


安裝第一個container

直接使用git bash可以呼叫到docker 和docker-compose命令,直接在git bash上執行
(使用90端口,避免和XAMPP的80端口衝突)
$ docker run -d -p 90:80 docker/getting-started
Unable to find image 'docker/getting-started:latest' locally
latest: Pulling from docker/getting-started
188c0c94c7c5: Pull complete
61c2c0635c35: Pull complete
378d0a9d4d5f: Pull complete
2fe865f77305: Pull complete
b92535839843: Pull complete
ebe280ac36f4: Pull complete
b7beeb601852: Pull complete
Digest: sha256:7434aacfacb3c2d7d49f013e8a8092fde022c8b9f058b7fae7077e9cad1edc99
Status: Downloaded newer image for docker/getting-started:latest
661e2d85e8b11aad23994a53d869fd476a18e5524ebc442c2884ba0825cb9f59

然後就可以在 Docker Desktop 看到新的containers和images


在 Containers / Apps 中對執行中的container 點擊【open  in browser 】可以直接在瀏覽器打開docker/getting-started 容器的頁面


laradock

build容器時因為網路不好報錯:
Step 146/294 : RUN if [ ${INSTALL_NODE} = true ]; then     mkdir -p $NVM_DIR &&     curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash         && . $NVM_DIR/nvm.sh         && nvm install ${NODE_VERSION}         && nvm use ${NODE_VERSION}         && nvm alias ${NODE_VERSION}         && if [ ${NPM_REGISTRY} ]; then         npm config set registry ${NPM_REGISTRY}         ;fi         && if [ ${INSTALL_NPM_GULP} = true ]; then         npm install -g gulp         ;fi         && if [ ${INSTALL_NPM_BOWER} = true ]; then         npm install -g bower         ;fi         && if [ ${INSTALL_NPM_VUE_CLI} = true ]; then         npm install -g @vue/cli         ;fi         && if [ ${INSTALL_NPM_ANGULAR_CLI} = true ]; then         npm install -g @angular/cli         ;fi
     && ln -s `npm bin --global` /home/laradock/.node-bin ;fi
...
npm ERR! code ECONNRESET
npm ERR! network aborted
npm ERR! network This is a problem related to network connectivity.
npm ERR! network In most cases you are behind a proxy or have bad network settings.
npm ERR! network
npm ERR! network If you are behind a proxy, please make sure that the
npm ERR! network 'proxy' config is set properly.  See: 'npm help config'

解法

Windows 打開proxy和shadowsocks,換一個IP重新build就可以了

XDebug

放棄,搞不定。還是用hyper-v中的laradock斷點(HTTP+CLI都可以)