GO LANG FOR PHP PROGRAMMER

Function

Fonksiyon Tanımlama

package main
func Sum(a int, b int) int {
    return a + b
}
<?php
function Sum($a,$b){
    $a=(int)$a;
    $b=(int)$b;
    return (int)$a+$b;
}
?>

Fonksiyonlarda Çoklu Sonuç Döndürme

package main

import "fmt"

func Run(a int, b int) (int, int) {
    return a + b, a * b
}
func main() {
    sum, multiplication := Run(1, 2)
    fmt.Println(sum)
    fmt.Println(multiplication)
}
<?php
function Run($a,$b){
    $a=(int)$a;
    $b=(int)$b;
    return array((int)$a+$b,(int)$a*b);
}

$data=Run(1,2);
$sum=$data[0];
$multiplication=$data[1];
echo $sum;
echo "\n";
echo $multiplication;

Global Variable

package main

import "fmt"

var x int = 10

func Vada(a int) int {
    return a + x
}
func VadaVada(a int) int {
    x := 20 // You see? // var x int = 10
    return a + x
}
func VadaVadaVada(a int) int {
    x = 30 // You see? global x is changed .. global x değeri değiştirildi.
    return a + x
}
func main() {
    fmt.Println(Vada(11))

    fmt.Println(VadaVada(11))
    fmt.Println(x)

    fmt.Println(VadaVadaVada(11))
    fmt.Println(x)

}
<?php
$x=10;
function Vada($a){
    global $x;
    $a=(int)$a;
    return (int)$a+$x;
}
function VadaVada($a){
    $x=20;
    $a=(int)$a;
    return (int)$a+$x;
}
function VadaVadaVada($a){
    global $x;
    $x=30;
    $a=(int)$a;
    return (int)$a+$x;
}
echo Vada(11)."\n";
echo VadaVada(11)."\n";
echo $x."\n";
echo VadaVadaVada(11)."\n";
echo $x."\n";