Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
DesignOneGo01-PHPerのためのGo入門
Search
ssttkknn
May 30, 2019
Technology
0
450
DesignOneGo01-PHPerのためのGo入門
DesignOneGo01
PHPerのためのGo入門
ssttkknn
May 30, 2019
Tweet
Share
Other Decks in Technology
See All in Technology
[ JAWS-UG 東京 CommunityBuilders Night #2 ]SlackとAmazon Q Developerで 運用効率化を模索する
sh_fk2
3
410
会社紹介資料 / Sansan Company Profile
sansan33
PRO
6
380k
Rustから学ぶ 非同期処理の仕組み
skanehira
1
130
AWSを利用する上で知っておきたい名前解決のはなし(10分版)
nagisa53
10
3.1k
テストを軸にした生き残り術
kworkdev
PRO
0
200
【実演版】カンファレンス登壇者・スタッフにこそ知ってほしいマイクの使い方 / 大吉祥寺.pm 2025
arthur1
1
830
20250910_障害注入から効率的復旧へ_カオスエンジニアリング_生成AIで考えるAWS障害対応.pdf
sh_fk2
3
240
dbt開発 with Claude Codeのためのガードレール設計
10xinc
2
1.2k
250905 大吉祥寺.pm 2025 前夜祭 「プログラミングに出会って20年、『今』が1番楽しい」
msykd
PRO
1
840
職種の壁を溶かして開発サイクルを高速に回す~情報透明性と職種越境から考えるAIフレンドリーな職種間連携~
daitasu
0
160
KotlinConf 2025_イベントレポート
sony
1
130
Firestore → Spanner 移行 を成功させた段階的移行プロセス
athug
1
470
Featured
See All Featured
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
8
920
How GitHub (no longer) Works
holman
315
140k
The Language of Interfaces
destraynor
161
25k
Designing Experiences People Love
moore
142
24k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
26
3k
Code Reviewing Like a Champion
maltzj
525
40k
Large-scale JavaScript Application Architecture
addyosmani
512
110k
Typedesign – Prime Four
hannesfritz
42
2.8k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
31
2.2k
Imperfection Machines: The Place of Print at Facebook
scottboms
268
13k
Stop Working from a Prison Cell
hatefulcrawdad
271
21k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
12
1.1k
Transcript
PHPerのためのGo入門 satoken
自己紹介 ベトナムの人 PHP歴:約2.5年 Go歴:カイゼン会でやった程度
この発表の対象 PHPは触ったことがあるけど、Goは触ったことがない人 ほんのちょっとだけGo触ったことがあるけど忘れちゃった人 この発表の対象じゃない人 Goに自信ニキ Gopherくんでも愛でながらお待ちください 注 PHPとGoの比較をメインに置いており、 Go独自のものにはそれほど触れていません
None
khởi đầu ベトナム語でstartの意
変数 変数定義 PHP $name; 型は不要 Go var name String 型が必要
変数初期化 PHP $name = "satoken"; Go var name String =
"satoken" /* 短縮宣言 関数の内部でしか使用できない */ name := "satoken"
配列 添字配列 PHP $array = []; Go // array 長さを指定した静的な配列
var array [10]int // slice 長さを指定しない動的な配列 var slice []int
Array 長さも配列の一部 [3]int と [4]int は別物 配列間の代入は値渡し 長さを ... で省略することもできる
a := [...]int{1,2,3} Slice 配列間の代入は参照渡し
連想配列(ディクショナリ) PHP $dict = ["a" => 1, "b" => 2];
添字配列をちょっと拡張したもの Go var dict map[string]int dict = make(map[string]int) dict["a"] = 1 dict["b"] = 2 dict := map[string]int{"a":1, "b":2} map と明示する必要がある
Map keyが存在するか確認する mapは2つの戻り値があり、2つ目の戻り値では、keyが存在するか どうかを返す ageMap := map[string]int{"Sawaya":23, "Sato":26} sawayaAge, hasKey
:= ageMap["Sawaya"] if hasKey { fmt.Println("ageMap は'Sawaya' というkey を持っています") } else { fmt.Println("ageMap は'Sawaya' というkey を持っていません") }
フロー if PHP if ($sawaya > 170) { echo("sawaya は170
より大きいです"); } else { echo("sawaya は170 以下です"); } Go if sawaya > 170 { fmt.Println("sawaya は170 より大きいです") } else { fmt.Println("sawaya は170 以下です") }
ループ PHP $sum = 0; for ($i = 0; $i
< 10; $i++) { $sum += $i; } Go sum := 0 for i :=0; i < 10; i++ { sum += i } 初期化; 条件式; 後処理 の形自体はPHPもGoも同じ
PHP foreach ($map as $key => $value) { echo("key:", $key,
"value:", $value); } Go for key, value := range map { fmt.Println("key:", key, " value:", value) }
PHP $n = 0; while (n < 10) { n++;
} Go n := 0 for n < 10 { n++ } 初期化と後処理を省略できる
for文の基本的な書き方はPHPとほぼ変わらない foreach, whileは存在しないが、forでまかなうことができる
switch PHP switch ($i) { case 0: // something to
do break; case 1: case 2: case 3: // something to do break; default: // something to do }
Go switch $i { case 0: // something to do
case 1, 2, 3: // something to do default: // something to do } breakを書かなくてもデフォルトでbreakする 複数の値を条件とするときはまとめることができる 意図的に他のcaseを実行したいときは fallthrough を使う
関数 返り値が1つ PHP function max(int $a, int $b) : int
{ if ($a > $b) { return $a; } return $b } Go func max(a, b int) int { if a > b { return a } return b }
返り値が複数 PHP // 複数の返り値をサポートしていないため配列で返す function sumAndSubtract(int $a, int $b) :
array { return [$a + $b, $a - $b]; } Go func sumAndSubtract(a, b int) (int, int) { return a + b, a - b }
可変長引数 PHP function count(int ...$numbers) { foreach ($numbers as $number)
{ echo($number); } } Go func count(numbers ...int) { for _, number := range numbers { fmt.Printf(number) }
参照渡し(ポインタ) PHP function add1(int &$a): int { return $a++; }
$num = 3; echo("num = ", $num) // "num = 3" $num1 = add1($num); echo("num1 = ", $num1) // "num1 = 4" echo("num = ", $num) // "num = 4"
Go package main import "fmt" func add1(a *int) int {
*a += 1 return *a } func main() { num := 3 fmt.Println("num = ", num) // "num = 3" num1 := add1(&num) fmt.Println("num = ", num1) // "num1 = 4" fmt.Println("num = ", num) // "num = 4" }
構造体 構造体の宣言 PHP 構造体という概念がないのでクラスで代替 class Person { private $name; private
$age; function __construct(string $name, int $age) { $this->name = $name; $this->age = $age; } // getter 省略 } $man = new Person("Sawaya", 23); echo("I am ", $man->getName()); echo("My age is ", $man->getAge());
Go type person struct { name string age int }
var man person man.name = "sawaya" man.age = 23 fmt.Println("I am ", man.name) fmt.Println("My age is ", man.age)
メソッド 構造体という概念がないのでクラスで代替 PHP class Rectangle { private $width; private $height;
public function __construct( float $width, float $height) { $this->width = $width; $this->height = $height; } private function area() : float { return $this->width * $this->height; } // getter 省略 } $square = new Rectangle(12, 2); echo("Area of Square is: ", $square->area());
Go type Rectangle struct { width, height float64 } func
(r Rectangle) area() float64 { return r.width * r.height } square := Rectangle{12, 2} fmt.Println("Area of Square is: ", square.area())
所感 冷静に見比べてみるとめちゃくちゃ違うというほどではない 型 変数名 と 変数名 型 の違いの違和感は大きいもののきっ と慣れるでしょう 時間を割くべきはGo独自のところ
構造体の埋め込み defer エラー時の処理 (Panic, Recover) 並列処理
Cảm ơn bạn rất nhiều ありがとうございましたの意