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
460
DesignOneGo01-PHPerのためのGo入門
DesignOneGo01
PHPerのためのGo入門
ssttkknn
May 30, 2019
Tweet
Share
Other Decks in Technology
See All in Technology
22nd ACRi Webinar - NTT Kawahara-san's slide
nao_sumikawa
0
100
外部キー制約の知っておいて欲しいこと - RDBMSを正しく使うために必要なこと / FOREIGN KEY Night
soudai
PRO
12
5.6k
モダンUIでフルサーバーレスなAIエージェントをAmplifyとCDKでサクッとデプロイしよう
minorun365
4
220
レガシー共有バッチ基盤への挑戦 - SREドリブンなリアーキテクチャリングの取り組み
tatsukoni
0
220
Introduction to Sansan, inc / Sansan Global Development Center, Inc.
sansan33
PRO
0
3k
OWASP Top 10:2025 リリースと 少しの日本語化にまつわる裏話
okdt
PRO
3
820
こんなところでも(地味に)活躍するImage Modeさんを知ってるかい?- Image Mode for OpenShift -
tsukaman
1
160
ClickHouseはどのように大規模データを活用したAIエージェントを全社展開しているのか
mikimatsumoto
0
260
Context Engineeringが企業で不可欠になる理由
hirosatogamo
PRO
3
620
超初心者からでも大丈夫!オープンソース半導体の楽しみ方〜今こそ!オレオレチップをつくろう〜
keropiyo
0
110
プロポーザルに込める段取り八分
shoheimitani
1
470
AIと新時代を切り拓く。これからのSREとメルカリIBISの挑戦
0gm
1
2.8k
Featured
See All Featured
Noah Learner - AI + Me: how we built a GSC Bulk Export data pipeline
techseoconnect
PRO
0
110
Deep Space Network (abreviated)
tonyrice
0
49
Leo the Paperboy
mayatellez
4
1.4k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
254
22k
Measuring Dark Social's Impact On Conversion and Attribution
stephenakadiri
1
130
A brief & incomplete history of UX Design for the World Wide Web: 1989–2019
jct
1
300
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
359
30k
Agile that works and the tools we love
rasmusluckow
331
21k
Design in an AI World
tapps
0
140
The Spectacular Lies of Maps
axbom
PRO
1
520
Darren the Foodie - Storyboard
khoart
PRO
2
2.4k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
249
1.3M
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 ありがとうございましたの意