$30 off During Our Annual Pro Sale. View Details »
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
DesignOneGo01-PHPerのためのGo入門
Search
ssttkknn
May 30, 2019
Technology
0
460
DesignOneGo01-PHPerのためのGo入門
DesignOneGo01
PHPerのためのGo入門
ssttkknn
May 30, 2019
Tweet
Share
Other Decks in Technology
See All in Technology
Sansanが実践する Platform EngineeringとSREの協創
sansantech
PRO
2
930
Strands Agents × インタリーブ思考 で変わるAIエージェント設計 / Strands Agents x Interleaved Thinking AI Agents
takanorig
2
180
年間40件以上の登壇を続けて見えた「本当の発信力」/ 20251213 Masaki Okuda
shift_evolve
PRO
1
140
NIKKEI Tech Talk #41: セキュア・バイ・デザインからクラウド管理を考える
sekido
PRO
0
150
ウェルネス SaaS × AI、1,000万ユーザーを支える 業界特化 AI プロダクト開発への道のり
hacomono
PRO
0
140
SQLだけでマイグレーションしたい!
makki_d
0
630
re:Invent 2025 ~何をする者であり、どこへいくのか~
tetutetu214
0
220
Power of Kiro : あなたの㌔はパワステ搭載ですか?
r3_yamauchi
PRO
0
180
IAMユーザーゼロの運用は果たして可能なのか
yama3133
2
490
ExpoのインダストリーブースでみたAWSが見せる製造業の未来
hamadakoji
0
150
MariaDB Connector/C のcaching_sha2_passwordプラグインの仕様について
boro1234
0
490
AI時代の新規LLMプロダクト開発: Findy Insightsを3ヶ月で立ち上げた舞台裏と振り返り
dakuon
0
210
Featured
See All Featured
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
37
2.7k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
27k
GraphQLの誤解/rethinking-graphql
sonatard
73
11k
Side Projects
sachag
455
43k
The Illustrated Children's Guide to Kubernetes
chrisshort
51
51k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.6k
Statistics for Hackers
jakevdp
799
230k
Scaling GitHub
holman
464
140k
How to train your dragon (web standard)
notwaldorf
97
6.4k
Navigating Team Friction
lara
191
16k
Imperfection Machines: The Place of Print at Facebook
scottboms
269
13k
Into the Great Unknown - MozCon
thekraken
40
2.2k
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 ありがとうございましたの意