20200701のGoに関する記事は6件です。

WSL2+VSCode+Docker Desktop

windows10とVSCodeでWindows上のフォルダで作業をしながらDockerを利用したデバッグ開発ができるようになったので設定までの流れを書き留めます。
windowsの環境を開発ツールのインストールで汚すことがなくなりスッキリし、ワークフォルダ毎に開発環境を切り替えることができるようになります。

WSL2を使用するためWindows10 2004にアップグレード (既にWindows10 2004の場合はスキップ)

https://www.microsoft.com/ja-jp/software-download/windows10

無題.png

Linuxカーネルを更新する

https://docs.microsoft.com/ja-jp/windows/wsl/wsl2-kernel

PowerShellを開いて既存のディストリビューションをWSL2に設定

とりあえず既にインストールされているLinuxのディストリビューションをWSL2に設定します

確認

wsl --list --verbose

変更

wsl -set-version Ubuntu*** 2

無題.png

Docker DesktopをWSL2 base engineに設定 (Docker Desktopが入ってなければインストール)

無題.png

VSCodeのエクステンションで"Remote Development"をインストールする。(Remote WSL, Remote Containers, Remote SSHのパック)

無題.png

VSCodeのコンテナの設定ファイル (.devcontainer/devcontainer.json)

デバッグ環境イメージのDockerファイルの指定とコンテナからlocalhostへ通すポートの指定を行う

  • "dockerFile"で開発環境のDockerイメージを指定したDockerfile
  • "forwardPorts"でローカルへのポートフォワード指定

下の例ではコンテナへlocalhost:80でアクセスできるようになります

    // For format details, see https://aka.ms/vscode-remote/devcontainer.json or this file's README at:
    // https://github.com/microsoft/vscode-dev-containers/tree/v0.122.1/containers/docker-existing-dockerfile
    {
        "name": "Existing Dockerfile",
        // Sets the run context to one level up instead of the .devcontainer folder.
        "context": "..",
        // Update the 'dockerFile' property if you aren't using the standard 'Dockerfile' filename.
        "dockerFile": "../remotedev/Dockerfile",
        // Set *default* container specific settings.json values on container create.
        "settings": { 
            "terminal.integrated.shell.linux": null
        },
        // Add the IDs of extensions you want installed when the container is created.
        "extensions": [],
        // Use 'forwardPorts' to make a list of ports inside the container available locally.
        "forwardPorts": [80]
        // Uncomment the next line to run commands after the container is created - for example installing curl.
        // "postCreateCommand": "apt-get update && apt-get install -y curl",
        // Uncomment when using a ptrace-based debugger like C++, Go, and Rust
        // "runArgs": [ "--cap-add=SYS_PTRACE", "--security-opt", "seccomp=unconfined" ],
        // Uncomment to use the Docker CLI from inside the container. See https://aka.ms/vscode-remote/samples/docker-from-docker.
        // "mounts": [ "source=/var/run/docker.sock,target=/var/run/docker.sock,type=bind" ],
        // Uncomment to connect as a non-root user. See https://aka.ms/vscode-remote/containers/non-root.
        // "remoteUser": "vscode"
}

VSCode起動設定 (.vscode/launch.json)

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch",
            "type": "go",
            "request": "launch",
            "mode": "auto",
            "program": "${workspaceRoot}",
            "env": {
            },
            "args": [],
            "buildFlags": "-tags=debug",
            "args": [],
            "showLog": true
        },
        {
            "name": "Remote",
            "type": "go",
            "request": "launch",
            "mode": "remote",
            "remotePath": "/go/src/app", 
            "program": "${workspaceRoot}",
            "env": {
            },
            "args": [],
            "buildFlags": "-tags=debug",
            "showLog": true
        }        
    ]
}

ワークフォルダ/remotedev/Dockerfile (下のイメージはプライベートなDockerイメージのため存在しません)

FROM golang-aws-alpine:1.13-devel

プライベート環境にDockerレジストリがあれば開発環境をライブラリ化していくことができるようになり便利です。

VSCodeのリモートエクスプローラーでコンテナを起動する

無題.png

コンテナ側のgoエクステンションを有効にする

無題.png

  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

GO言語 複数のマップをマージ する

逆引きGolangには2つのマップをマージするやり方が書いてあったが、
今回は2つ以上のマップをマージする可変長引数関数を作ってみた。
これにより、マップを幾つでもマージしてくれる。

func merge(m ...map[string]interface{}) map[string]interface{} {
    ans := make(map[string]interface{}, 0)

    for _, c := range m {
        for k, v := range c {
            ans[k] = v
        }
    }
    return ans
}

参考文献

逆引きGolang (https://ashitani.jp/golangtips/tips_map.html#map_Merge)

  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

goland 設定go ModulesのEnable Vendoring Support automaticllyを外さないと

問題

JetBrain goland中に
テストコードを実行する時に以下のエラーが発生されました。

./somefile.go:3:8: cannot find package "." in:
    /project/vendor/somepackage

パッケージ先がgo moduleに指定されたあるはずですが、vendorに指定されて、vendorにパーケージがないから、見つからないエラーが発生されます。

解決法

設定
->go
->go Modules
->Enable Vendoring Support automaticllyを外したら、正常に戻ります。

スクリーンショット 2020-07-01 16.03.26.png

  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

Jenkinsのビルドが突然java.net.MalformedURLExceptionで落ちるようになった

はじめに

JenkinsでGoで書かれたソースをビルドしているのですが、そのジョブが突然落ちるようになったので解決策を記載します。

この問題は、記載している時点(2020/07/01)で既に修正済み(リリース待ち)の状態です。
リリースまでの一時しのぎとして対応していますのでご注意ください。

エラー内容

昨日(2020/06/30)あたりから、突然Jenkinsが以下の例外を吐いて落ちるようになりました。

java.net.MalformedURLException: no protocol: /dl/go1.11.2.linux-amd64.tar.gz
    at java.net.URL.<init>(URL.java:593)
    at java.net.URL.<init>(URL.java:490)
    at java.net.URL.<init>(URL.java:439)
    at org.jenkinsci.plugins.golang.GolangInstaller.performInstallation(GolangInstaller.java:57)
    at hudson.tools.InstallerTranslator.getToolHome(InstallerTranslator.java:69)
    at hudson.tools.ToolLocationNodeProperty.getToolHome(ToolLocationNodeProperty.java:109)
    at hudson.tools.ToolInstallation.translateFor(ToolInstallation.java:206)
    at org.jenkinsci.plugins.golang.GolangInstallation.forNode(GolangInstallation.java:44)
    at org.jenkinsci.plugins.golang.GolangInstallation.forNode(GolangInstallation.java:22)
    at org.jenkinsci.plugins.workflow.steps.ToolStep$Execution.run(ToolStep.java:152)
    at org.jenkinsci.plugins.workflow.steps.ToolStep$Execution.run(ToolStep.java:133)
    at org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution.lambda$start$0(SynchronousNonBlockingStepExecution.java:47)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
Finished: FAILURE

ググってみると以下のページがヒット。

https://issues.jenkins-ci.org/browse/JENKINS-62887

どうやらJenkins内でGoをインストールする際のURLが正しくないようです。
この修正は修正は完了していてリリース待ちの状態とのこと。とはいえ、実装したソースをすぐデプロイして確認したかったので、リリースまでの間取りあえず動く状態にします。

Jenkinsの管理 → Global Tool Configuration と遷移し、Goの設定を開きます。(インストール済みGo...のボタンをクリックします)
私は以下のようにGoを自動インストールする設定にしていたので、この自動インストール時に落ちていそうです。

スクリーンショット 2020-07-01 11.03.37.png

よって、自動インストールを停止して、すでにインストール済みのGoのディレクトリを指定する形にして対処しました。
プラグインによって自動ダウンロードされたものは、${JENKINS_HOME/tools/org.jenkinsci.plugins.golang.GolangInstallation配下にインストールされているのでそれを指定します。

スクリーンショット 2020-07-01 11.12.24.png

直近で他のバージョンのGoを新規インストールする予定はないのでこれで十分です。

  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

無料で1日でサービスを開始できる時代。ただ、ユーザーをどう増やすか。

最近、暇で以下のサービスを作った。
https://deau-project.herokuapp.com/

3日でサービスを開始した。
こんな感じ。

1日目: 何を作るのか。環境はどうするか。
=> メールで出会うサービスで、Heroku, React, Go Gin に決定してデプロイした。
https://devcenter.heroku.com/articles/getting-started-with-go

2日目: React でデザインから実装。
=> 以下のテンプレートを組み合わせてフォームを実装した。
https://github.com/mui-org/material-ui/tree/master/docs/src/pages/getting-started/templates/album
https://github.com/mui-org/material-ui/tree/master/docs/src/pages/getting-started/templates/sign-in
https://react-hook-form.com/jp/

3日目: Go Gin でメール送信を実装。あと、バグ修正。

色々と悩みながら実装したので、これを、悩みの時間抜きにギュッとすると一日で実装できる。

今日はサービス開始の2日目。
リリースと同時に、ブログを書いたので、そのリンクからユーザーが50人ぐらい来て、12人ほどメール送信してくれた。

さて、これからどうやってユーザーを増やすか。
無料でやるなら、SNSやブログ。
ただ、時間がかかる。

やはり、広告しかないのか。

  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

goの構造体のJSONのtagでめちゃくちゃ時間を溶かした

goの構造体のJSONのtagでめちゃくちゃ時間を溶かした

題目の通りです。2時間くらい溶かしました。

なにでそんなに溶かしたのか

jsonのTagでそんなに溶かすことないだろ、と。溶かした原因はこれです

example.go
type JsonValue struct {
  foo string `json:"foo"`
  bar string `json:"bar, omitempty"`
}

お分かり頂けただろうか?この心霊現象。。
わからない人は僕と一緒に2時間溶かすかも知れない恐ろしいものです。。

bar string `json:"bar, omitempty"`

スペースですね。カンマの後ろのスペースです。時間を溶かしたのはこれエラー吐かないからです。
stringなら空文字が入っていたときに json.Marshal すると bar は省略されるはずなのに空文字が入っちゃっておかしいなぁ〜ってなります。
このスペースは入れてはいけません。正しくはこう

bar string `json:"bar,omitempry"`

こんな簡単なミスにハマる人がいるかは分かりませんが、もしいたら助けになればと思います。
僕はもう一生忘れません。

  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む