以下は、Go言語とCockroachDBを使用したGUI勘定系システムの例です。このシステムは、ユーザーがカードを挿入し、暗証番号を入力して、残高確認、入金、出金、振込などの操作を行えるようにします。GUIには`fyne`ライブラリを使用し、データベースにはCockroachDBを使用します。
### 1. データベースの準備
まず、CockroachDBに`accounts`テーブルを作成します。
```sql
CREATE TABLE accounts (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
card_number TEXT NOT NULL UNIQUE,
pin TEXT NOT NULL,
balance DECIMAL(10, 2) NOT NULL
);
```
### 2. Goプロジェクトの設定
Goプロジェクトを作成し、必要なライブラリをインストールします。
```bash
go mod init account_system
go get fyne.io/fyne/v2
go get github.com/lib/pq
```
### 3. Goコードの実装
以下は、GoでGUI勘定系システムを実装するコード例です。
```go
package main
import (
"database/sql"
"fmt"
"log"
"strconv"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/widget"
_ "github.com/lib/pq"
)
type Account struct {
ID string
CardNumber string
PIN string
Balance float64
}
var db *sql.DB
func main() {
// Initialize database connection
initDB()
// Create Fyne application
a := app.New()
w := a.NewWindow("Account Management System")
w.Resize(fyne.NewSize(400, 300))
// Card insertion
cardEntry := widget.NewEntry()
cardEntry.SetPlaceHolder("Insert your card number")
// PIN entry
pinEntry := widget.NewPasswordEntry()
pinEntry.SetPlaceHolder("Enter your 4-digit PIN")
// Login button
loginButton := widget.NewButton("Login", func() {
cardNumber := cardEntry.Text
pin := pinEntry.Text
account, err := authenticate(cardNumber, pin)
if err != nil {
dialog.ShowError(fmt.Errorf("Authentication failed"), w)
return
}
showMainMenu(w, account)
})
// Layout
loginContainer := container.NewVBox(
widget.NewLabel("Welcome to the Account Management System"),
cardEntry,
pinEntry,
loginButton,
)
w.SetContent(loginContainer)
w.ShowAndRun()
}
func initDB() {
var err error
connStr := "postgresql://root@localhost:26257/bank?sslmode=disable"
db, err = sql.Open("postgres", connStr)
if err != nil {
log.Fatal(err)
}
err = db.Ping()
if err != nil {
log.Fatal(err)
}
fmt.Println("Connected to CockroachDB")
}
func authenticate(cardNumber, pin string) (*Account, error) {
var account Account
query := `SELECT id, card_number, pin, balance FROM accounts WHERE card_number = $1 AND pin = $2`
err := db.QueryRow(query, cardNumber, pin).Scan(&account.ID, &account.CardNumber, &account.PIN, &account.Balance)
if err != nil {
return nil, err
}
return &account, nil
}
func showMainMenu(w fyne.Window, account *Account) {
// Main menu options
balanceButton := widget.NewButton("Check Balance", func() {
showBalance(w, account)
})
depositButton := widget.NewButton("Deposit", func() {
showDeposit(w, account)
})
withdrawButton := widget.NewButton("Withdraw", func() {
showWithdraw(w, account)
})
transferButton := widget.NewButton("Transfer", func() {
showTransfer(w, account)
})
// Layout
menuContainer := container.NewVBox(
widget.NewLabel("Main Menu"),
balanceButton,
depositButton,
withdrawButton,
transferButton,
)
w.SetContent(menuContainer)
}
func showBalance(w fyne.Window, account *Account) {
balanceLabel := widget.NewLabel(fmt.Sprintf("Your current balance is: $%.2f", account.Balance))
printButton := widget.NewButton("Print Statement", func() {
dialog.ShowInformation("Print", "Statement printed successfully", w)
w.Close()
})
noPrintButton := widget.NewButton("No Print", func() {
dialog.ShowInformation("Thank You", "Thank you for using our service", w)
w.Close()
})
// Layout
balanceContainer := container.NewVBox(
balanceLabel,
printButton,
noPrintButton,
)
w.SetContent(balanceContainer)
}
func showDeposit(w fyne.Window, account *Account) {
amountEntry := widget.NewEntry()
amountEntry.SetPlaceHolder("Enter deposit amount")
confirmButton := widget.NewButton("Confirm", func() {
amount, err := strconv.ParseFloat(amountEntry.Text, 64)
if err != nil {
dialog.ShowError(fmt.Errorf("Invalid amount"), w)
return
}
account.Balance += amount
updateBalance(account)
dialog.ShowInformation("Deposit", fmt.Sprintf("Deposit successful. New balance: $%.2f", account.Balance), w)
showMainMenu(w, account)
})
// Layout
depositContainer := container.NewVBox(
widget.NewLabel("Enter deposit amount:"),
amountEntry,
confirmButton,
)
w.SetContent(depositContainer)
}
func showWithdraw(w fyne.Window, account *Account) {
amountEntry := widget.NewEntry()
amountEntry.SetPlaceHolder("Enter withdrawal amount")
confirmButton := widget.NewButton("Confirm", func() {
amount, err := strconv.ParseFloat(amountEntry.Text, 64)
if err != nil {
dialog.ShowError(fmt.Errorf("Invalid amount"), w)
return
}
if amount > account.Balance {
dialog.ShowError(fmt.Errorf("Insufficient balance"), w)
return
}
account.Balance -= amount
updateBalance(account)
dialog.ShowInformation("Withdrawal", fmt.Sprintf("Withdrawal successful. New balance: $%.2f", account.Balance), w)
showMainMenu(w, account)
})
// Layout
withdrawContainer := container.NewVBox(
widget.NewLabel("Enter withdrawal amount:"),
amountEntry,
confirmButton,
)
w.SetContent(withdrawContainer)
}
func showTransfer(w fyne.Window, account *Account) {
bankEntry := widget.NewEntry()
bankEntry.SetPlaceHolder("Enter bank name")
branchEntry := widget.NewEntry()
branchEntry.SetPlaceHolder("Enter branch name")
accountTypeEntry := widget.NewEntry()
accountTypeEntry.SetPlaceHolder("Enter account type (普通 or 当座)")
accountNumberEntry := widget.NewEntry()
accountNumberEntry.SetPlaceHolder("Enter account number")
accountNameEntry := widget.NewEntry()
accountNameEntry.SetPlaceHolder("Enter account holder name")
amountEntry := widget.NewEntry()
amountEntry.SetPlaceHolder("Enter transfer amount")
confirmButton := widget.NewButton("Confirm", func() {
amount, err := strconv.ParseFloat(amountEntry.Text, 64)
if err != nil {
dialog.ShowError(fmt.Errorf("Invalid amount"), w)
return
}
if amount > account.Balance {
dialog.ShowError(fmt.Errorf("Insufficient balance"), w)
return
}
account.Balance -= amount
updateBalance(account)
dialog.ShowInformation("Transfer", fmt.Sprintf("Transfer successful. New balance: $%.2f", account.Balance), w)
showMainMenu(w, account)
})
// Layout
transferContainer := container.NewVBox(
widget.NewLabel("Enter transfer details:"),
bankEntry,
branchEntry,
accountTypeEntry,
accountNumberEntry,
accountNameEntry,
amountEntry,
confirmButton,
)
w.SetContent(transferContainer)
}
func updateBalance(account *Account) {
query := `UPDATE accounts SET balance = $1 WHERE id = $2`
_, err := db.Exec(query, account.Balance, account.ID)
if err != nil {
log.Fatal(err)
}
}
```
### 4. 実行方法
1. CockroachDBを起動し、`accounts`テーブルを作成します。
2. Goプロジェクトを設定し、必要なライブラリをインストールします。
3. 上記のGoコードをプロジェクトに追加し、`main.go`を実行します。
### 5. 動作確認
- カード番号と暗証番号を入力してログインします。
- ログイン後、残高確認、入金、出金、振込などの操作を選択できます。
- 各操作が正しくデータベースに反映されることを確認します。
この例は基本的な機能を実装していますが、実際のシステムではさらにエラーハンドリングやセキュリティ対策を強化する必要があります。
任意の電子回路をシュミレーションし、PLCコードに置き換える画期的なアプリ「Simulink PLC Coder」



Q.1 入力波がAsinωtであるコレクタ接地回路と出力波をGo言語で表してほしい。
Go言語で入力波形を模倣して出力するプログラムを作成する場合、PLCの実際の入出力と接続することは難しいですが、シミュレーションとして入力波形を生成し、出力として模倣する形でコレクタ接地回路の動作を表すことができます。
以下のコードでは、単純に入力波形をサイン波として生成し、それをそのまま出力する形でコレクタ接地回路の動作を再現しています。シミュレーションのため、サンプリング間隔ごとに値を取得して出力する処理を行っています。
```go
package main
import (
"fmt"
"math"
"time"
)
// 定数
const (
Amplitude = 1.0 // 振幅
Frequency = 1.0 // 周波数 (Hz)
SampleRate = 100 // サンプリングレート (サンプル数/秒)
)
func main() {
// サンプリング周期を計算
sampleInterval := 1.0 / SampleRate
t := 0.0
// シミュレーション実行
for i := 0; i < SampleRate*5; i++ { // 5秒間のサンプリング
// 入力波形生成 (A*sin(ωt))
input := Amplitude * math.Sin(2*math.Pi*Frequency*t)
// コレクタ接地回路の出力 (エミッタフォロワーを模倣)
output := input // 実際には少し減衰があるが、ここでは理想的な出力とする
// 結果を出力
fmt.Printf("Time: %.3f sec, Input: %.3f, Output: %.3f
", t, input, output)
// 時間を更新
t += sampleInterval
// サンプリング間隔に合わせてスリープ
time.Sleep(time.Duration(sampleInterval * float64(time.Second)))
}
}
```
### コードの解説
1. **定数の設定**: 振幅 (`Amplitude`)、周波数 (`Frequency`)、およびサンプリングレート (`SampleRate`) を設定します。これにより、入力波形が決定されます。
2. **サンプリング周期の計算**: サンプリング間隔は `1.0 / SampleRate` で計算され、`sampleInterval` に格納されます。
3. **ループでシミュレーション**: `for` ループで時間を進めながら、入力と出力の波形を生成して表示します。ここで、理想的なコレクタ接地回路と仮定し、入力波形をそのまま出力波形にしています。
4. **結果の出力**: `fmt.Printf` を使って、時刻、入力、出力の値を表示します。
5. **サンプリング間隔のスリープ**: 実際の時間間隔に合わせて `time.Sleep` を使用しています。
このコードを実行すると、入力波形(サイン波)と出力波形が時間経過に応じて表示され、理想的なエミッタフォロワー回路における入出力の関係が模倣されます。
Q.2 出力波のグラフを描いて
Go言語で波形のグラフを描画するには、グラフ描画のライブラリを使うのが便利です。代表的なライブラリとして `gonum/plot` があり、これを使用すると簡単に波形をプロットできます。
以下に、`gonum/plot` を使って入力波と出力波をプロットするコードを示します。このコードは、入力波形と出力波形を計算し、波形のグラフを `output.png` に保存します。
### 前提条件
`gonum/plot` ライブラリをインストールするには、以下のコマンドを実行してください:
```sh
go get gonum.org/v1/plot/...
```
### コード例
以下が `gonum/plot` を使って入力波形と出力波形を描画するサンプルコードです。
```go
package main
import (
"math"
"time"
"gonum.org/v1/plot"
"gonum.org/v1/plot/plotter"
"gonum.org/v1/plot/vg"
)
// 定数
const (
Amplitude = 1.0 // 振幅
Frequency = 1.0 // 周波数 (Hz)
SampleRate = 100 // サンプリングレート (サンプル数/秒)
Duration = 1.0 // グラフの表示時間 (秒)
)
func main() {
// サンプリング周期の計算
sampleInterval := 1.0 / SampleRate
numSamples := int(Duration * SampleRate)
// 入力波形と出力波形のデータポイントを格納するスライス
inputData := make(plotter.XYs, numSamples)
outputData := make(plotter.XYs, numSamples)
// 波形データを生成
t := 0.0
for i := 0; i < numSamples; i++ {
// 入力波形生成 (A * sin(ωt))
input := Amplitude * math.Sin(2*math.Pi*Frequency*t)
// コレクタ接地回路の出力 (エミッタフォロワーを模倣)
output := input
// データをスライスに格納
inputData[i].X = t
inputData[i].Y = input
outputData[i].X = t
outputData[i].Y = output
// 時間を更新
t += sampleInterval
}
// プロットの生成
p := plot.New()
p.Title.Text = "Input and Output Waveforms"
p.X.Label.Text = "Time (s)"
p.Y.Label.Text = "Amplitude"
// 入力波形のプロット
inputLine, err := plotter.NewLine(inputData)
if err != nil {
panic(err)
}
inputLine.Color = plotter.DefaultLineStyle.Color // デフォルトカラー
inputLine.LineStyle.Width = vg.Points(1)
// 出力波形のプロット
outputLine, err := plotter.NewLine(outputData)
if err != nil {
panic(err)
}
outputLine.Color = plotter.DefaultLineStyle.Color // 出力波の色
outputLine.LineStyle.Width = vg.Points(1)
outputLine.LineStyle.Dashes = []vg.Length{vg.Points(2), vg.Points(2)} // 出力波の線を破線に設定
// プロットに追加
p.Add(inputLine, outputLine)
p.Legend.Add("Input", inputLine)
p.Legend.Add("Output", outputLine)
// グラフの保存
if err := p.Save(8*vg.Inch, 4*vg.Inch, "output.png"); err != nil {
panic(err)
}
// 完了メッセージ
time.Sleep(1 * time.Second) // 描画完了を待つ
println("出力波のグラフが output.png として保存されました。")
}
```
### コードの解説
1. **定数の設定**: 振幅、周波数、サンプリングレート、表示時間を定義します。
2. **データ生成**: 指定されたサンプリングレートと時間範囲に基づいて、入力波形 ( A sin(omega t) ) を計算し、それを出力波形としてコピーしています。
3. **プロットの生成**: `gonum/plot` を使ってプロットを設定します。`plotter.NewLine` を使って `inputData` と `outputData` をプロットし、それぞれ異なる線のスタイル(入力は実線、出力は破線)で描画します。
4. **グラフの保存**: `output.png` ファイルとしてグラフを保存します。
### 実行結果
このコードを実行すると、`output.png` というファイルに入力波と出力波がプロットされます。理想的には、入力波と出力波は同じ波形になります。
サルでもわかるCOBOL入門 【ひよこグミ】 (plala.or.jp)
さんのCOBOLコードサンプルを
COBOL to Golang Converter (codeconvert.ai)
というトランスパイラサイト(Webアプリとも言う?)を使い、
Golangに書き換えてみた。
以下が元のCOBOLコードサンプル
IDENTIFICATION DIVISION.
PROGRAM-ID. CNT001S1.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 CNT PIC 9(03) VALUE 0.
PROCEDURE DIVISION.
A. ADD 1 TO CNT
DISPLAY "COUNT = " CNT
IF CNT = 100
THEN STOP RUN
ELSE GO TO A
END-IF.
がGolangでは
package main
import "fmt"
func main() {
var cnt int = 0
for {
cnt++
fmt.Println("COUNT =", cnt)
if cnt == 100 {
break
}
}
}
に書き換わり、
実行してみたら見事に成功!
ちなみにこのトランスパイラサイトはjavascriptコードがリンクされている。
その中身をちょっと覗いてみたら何かのAIのAPIキーを入れて使っているらしい。
webアプリじゃなくてネイティブアプリとして配布してほしい。
132件もの求人が出てきた。中にはtypescriptによるブロックチェーンの求人も。
ブロックチェーン技術はニーズが高いのが分かる。
この記事によると…
【金融庁のHPによると「仮想通貨」から「暗号資産」への呼称変更が提案されています。】
とのこと。
おい、金融庁!そんなことはどうでもいいんだよ!と言いたい。
すでにPythonでは仮想通貨取引に関する技術が出回っているようだが、Golangではまだまだ発展途上らしい。
速くてマルチタスクが得意なGolangの恩恵を受けたいものだ。
アプリのボタンみたいなやつをウィジェットという。
プログラミングを始める動機の大半はアプリを作りたいというものではなかろうか?
しかし、書店に行くとこの動機はコテンパンに打ち砕かれることになる。
クラスだのオブジェクト指向だの宣言するだの関数だの変数だの…
抽象的な言葉の羅列で、いったい何がしたいのだ?何をさせたいのだ?と言いたくなる。
俺がやりたいのはGUIアプリの作成だ!
ボタンを押すと何らかの動作がはじまる。

写真は西田尚美さんの若かった頃。
いやあ~こんなに美人でエロかったとは知らなかったwww
今日のオカズ、の前にこの記事読んでみそwww
もうすでにGo言語で自動売買プログラムを組んでる連中がゴロゴロいるらしい。
でも俺のコンセプトの
エリオット波動のフーリエ解析
→レンジとトレンド、トレンドとレンジの切り替わりの兆候を捉えAIで取引開始・停止(利確または損切り撤収)→逆指値トラップリピートEAにこの機能を搭載または連係
はどうかな?
さらにトレンド相場の時は逆指値トラップリピートEAを稼働させて指値トラップリピートEAは寝かせておき、レンジ相場の時は逆に指値トラップリピートEAを稼働させて逆指値トラップリピートEAは寝かせておくような機能を搭載したらどうなるかは火を見るより明らかだ。
フリーランスで人材不足となっている言語は何か?
そうだ!Golang、Go言語をやろう!
フリーランスとして案件を獲得したいなら絶対Go言語!JAVAの件数が最も多いが、とにかくGo!