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
360
DesignOneGo01-PHPerのためのGo入門
DesignOneGo01
PHPerのためのGo入門
ssttkknn
May 30, 2019
Tweet
Share
Other Decks in Technology
See All in Technology
Hands-on / Kaname Frusawa / Cloud Compare Users Meetup 2024 at University of Tokyo on April 17
paraworld
2
480
ChatGPT for IT Service Management (IT Pro)
dahatake
5
350
20240416_devopsdaystokyo
kzkmaeda
1
190
NgRx Signal Store
rainerhahnekamp
0
130
ChatworkのSRE部って実は 半分くらいPlatform Engineering部かもしれない
saramune
0
110
Vertex AI を中心に 生成AIのアップデートを共有します
kaz1437
0
180
Delivering Millions of Messages within seconds @ Duolingo
pelelgrino
0
340
Oracle Cloud Infrastructure:2024年4月度サービス・アップデート
oracle4engineer
PRO
1
120
自動生成を活用した、運用保守コストを抑える Error/Alert/Runbook の一元集約管理 / Centralized management of Error/Alert/Runbook to minimize operational costs using automated code generation
biwashi
10
2.2k
「手動オペレーションに定評がある」と言われた私が心がけていること / phpcon_odawara2024
blue_goheimochi
2
320
Tebiki株式会社 エンジニア採用資料
tebiki
0
4.1k
KubeCon EU 2024 Recap “Kubernetes Policy Time Machine: Where to Next?”
ryysud
0
150
Featured
See All Featured
Code Review Best Practice
trishagee
54
15k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
501
140k
RailsConf 2023
tenderlove
2
530
Designing for Performance
lara
601
67k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
15
1.4k
Unsuck your backbone
ammeep
662
57k
The Brand Is Dead. Long Live the Brand.
mthomps
48
28k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
321
20k
jQuery: Nuts, Bolts and Bling
dougneiner
59
7.1k
Stop Working from a Prison Cell
hatefulcrawdad
266
19k
A Modern Web Designer's Workflow
chriscoyier
688
190k
How to name files
jennybc
64
92k
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 ありがとうございましたの意