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
高速なプロダクト開発を実現、創業期から掲げるエンタープライズアーキテクチャ
kawauso
3
9.6k
オーティファイ会社紹介資料 / Autify Company Deck
autifyhq
10
130k
What’s new in Android development tools
yanzm
0
340
赤煉瓦倉庫勉強会「Databricksを選んだ理由と、絶賛真っ只中のデータ基盤移行体験記」
ivry_presentationmaterials
2
370
データグループにおけるフロントエンド開発
lycorptech_jp
PRO
1
110
american airlines®️ USA Contact Numbers: Complete 2025 Support Guide
supportflight
1
110
マーケットプレイス版Oracle WebCenter Content For OCI
oracle4engineer
PRO
3
960
AI時代の開発生産性を加速させるアーキテクチャ設計
plaidtech
PRO
3
160
React開発にStorybookとCopilotを導入して、爆速でUIを編集・確認する方法
yu_kod
1
290
Delta airlines®️ USA Contact Numbers: Complete 2025 Support Guide
airtravelguide
0
340
敢えて生成AIを使わないマネジメント業務
kzkmaeda
2
460
【LT会登壇資料】TROCCO新コネクタ「スマレジ」を活用した直営店データの分析
kazari0425
1
110
Featured
See All Featured
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.4k
YesSQL, Process and Tooling at Scale
rocio
173
14k
Building a Scalable Design System with Sketch
lauravandoore
462
33k
Typedesign – Prime Four
hannesfritz
42
2.7k
VelocityConf: Rendering Performance Case Studies
addyosmani
332
24k
StorybookのUI Testing Handbookを読んだ
zakiyama
30
5.9k
Practical Orchestrator
shlominoach
189
11k
Music & Morning Musume
bryan
46
6.6k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
35
2.4k
Building Applications with DynamoDB
mza
95
6.5k
Optimising Largest Contentful Paint
csswizardry
37
3.3k
A designer walks into a library…
pauljervisheath
207
24k
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 ありがとうございましたの意