shinpuuu’s blog

無職全裸限界中年(海外版)の怪奇日記

本日のやったことまとめ1

Giteaの使い方忘れた

PC変えたらリポジトリのクローンのできないし。ユーザー名とパスワードって何?なんも覚えてないが?

結果から言うとGitのアカウント名&パスワード(Git設定時にターミナルでgit config...などで設定)と、Giteaのアカウント名&パスワードには何の関係もなく、私が勝手に混同していただけだった。kood/JõhviのGiteaアカウントにアクセスするのに要求されているユーザー名とパスワードは、kood/JõhviのGiteaアカウント設定つまりあそこのユーザー名とパスワード(01Eduのものと同じ)である。

主に使うコマンドはこのあたりか:

git clone リポジトリURL // 現在のディレクトリに作成される
git add ファイル名など // gitで(コマンド実行時点での)このファイルを追跡する
git commit -m "コメント" // インラインでコメントするオプション
git push // 準備できたファイルをリポジトリに反映

この辺のエラーはGit - 変更内容のリポジトリへの記録で解決しそう。

以下はそのうち読んでおくということで…

VS Codeのターミナルで「code ファイル名」が通じない

なんか拡張入れてた気もするがもう覚えてねえ…検索してもVS Codeの”code”にキーワードが邪魔されてうまく調べられなくて草。

微妙に関係ないけど以下の記事に倣って、PowerShellからVS Code開いて($ code .)内部のターミナル使ったらなぜか「code ファイル名」使えるようになった。なんでや。

www.geeksforgeeks.org

Go言語基礎思い出し

go.modってなんだ?

とりあえず動かすにはこいつを生成する必要があるので:

go mod init リポジトリ名

でgo.modというファイルがディレクトリ内に作成される。

リポジトリ名の部分は非公開なら適当にファイル名でいいっぽいのだが、公開前提だとリポジトリURLと同じ形式が推奨らしい。 よくわからんかったので今回は git/アカウント名/リポジトリ名(ディレクトリ名) とした。まずかったら後で直してみる。

そしてモジュールとかこのgo.modとか何…っていうのは公式チュートリアルによると:

Enable dependency tracking for your code.

When your code imports packages contained in other modules, you manage those dependencies through your code's own module. That module is defined by a go.mod file that tracks the modules that provide those packages. That go.mod file stays with your code, including in your source code repository.

To enable dependency tracking for your code by creating a go.mod file, run the go mod init command, giving it the name of the module your code will be in. The name is the module's module path.

In actual development, the module path will typically be the repository location where your source code will be kept. For example, the module path might be . If you plan to publish your module for others to use, the module path must be a location from which Go tools can download your module. For more about naming a module with a module path, see Managing dependencies. github.com/mymodule

For the purposes of this tutorial, just use . example/hello

…いやわからん!日本語の包括的な説明を探してみた(後で読む):

ディレクトリ構成についてはこのへん(後で読む):

因みに書いたgoプログラムを動かすには以下の通り:

go run ~.go

ターミナルでのファイル操作

ターミナルで引数にファイルをとってgoで操作するためのos関数まわりの実装例。

package main

import (
    "fmt"
    "os"
)

func main() {
    args := os.Args
    argsLength := len(args)

    if argsLength == 3 {
        // check files exist
        for i := 1; i < argsLength; i++ {
            _, err := os.Open(os.Args[i])
            if err != nil {
                fmt.Printf("no such file: %v\n", err.Error())
                os.Exit(1)
            }
        }

        // if files exist
        fmt.Println("Successfully started")

    } else { // not enough or too many arguments
        fmt.Println("Please specify files to read and write.")
    }
}

注意としては、存在しない内容にアクセスしようとするとエラーになるので、それを避けてエラーメッセージ等を表示させる。 例えば引数無しの時 os.Args[1] を参照して何かと比較するとその if に入った時点でエラーになってしまうので、argsLengthで引数の数を記録しておきそれが足りない場合には条件節自体に入らないようにした。

エラー周りの挙動について

上でも扱っているこれは、Go言語ではよくある処理法らしい。errがnilでない、つまり「有る」場合の挙動を記述している。

_, err := os.Open(os.Args[i])
            if err != nil {
                fmt.Printf("no such file: %v\n", err.Error())
                os.Exit(1)
            }

参考:エラー(error型・errorsパッケージ)|Go言語入門

os.Exitについては以下を参照。

blog.logicoffee.tech

Go言語の命名規則

下手に他の言語も触ってたら何が何だか訳が分からなくなったのでありがたい。

Golangでの命名規則におけるベストプラクティス

明日はできれば課題1つ終わらせたい。無理ならテストだけでも作成しておきたいかな…