PHP To Go transition notes (1)

  golang, php

clipboard.png

Preface

As a PHP programmer, I feel honored. However, in the changing times, one must have sufficient knowledge to survive.

Let’s start with Go linguistics.

I hope you can have a basic understanding of Go after reading this article. This series of articles describes the way I learned Go language. The comparison between PHP code and Go code is used to distinguish and understand.

Variable

PHP

//Initialize variables
 $domain = "blog.fastrun.cn";
 //Batch Assignment
 $domain = $domain1 = $domain2 = "blog.fastrun.cn";

Go

//Initialize variables
 var domain string = "blog.fastrun.cn"
 //Batch Assignment
 var domain,domain1,domain2 string = "blog.fastrun.cn"
 //batch declaration assignment
 var username,age,local = "zhangsan",13,"BeiJing"
 var(
 username="zhangsan"
 age = 13
 local = "BeiJing"
 )

Constant

PHP

define("FOO","something");

Go

//Separate statement
 const FOO [string]= something
 //batch declaration
 const (
 USERNAME = "zhangsan"
 AGE      = 30
 )

Print

PHP

//Basic Output
 echo "blog.fastrun.cn";
 //print format
 printf("my blog %s","blog.fastrun.cn");

Go

//Basic Output
 fmt.Println("blog.fastrun.cn")
 //print format
 fmt.Printf("my blog %s","blog.fastrun.cn")

Function

PHP

//Basic Statement
 function printString(string $string){
 echo $string;
 }
 //with return value
 function printString(string $string) : string{
 return $string;
 }

Go

//Basic Statement
 func printString(s string){
 fmt.Println(s)
 }
 //with return value
 func printString(s string) string{
 return s
 }

Thank you

Thank you for seeing here. I hope this article can help you. Thank you