PHP程序员学习Go语言 转型手记

2018-10-3021:19:15编程语言入门到精通Comments3,349 views字数 926阅读模式

作为一名PHP程序员,我感到荣幸。但在时代不断的变迁中,要具备足够的知识才可生存。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/7581.html

那就从Go语言学起把。不知为什么,总感觉PHP与Go有很多相似之处。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/7581.html

希望看到本篇文章的你可以对Go有一个基本的认识。本系列文章与我自己学习Go语言的方式去描述。以PHP代码与Go代码的对比加以区分理解。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/7581.html

变量

PHP文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/7581.html

// 初始化变量
$domain = "blog.fastrun.cn";
// 批量赋值
$domain = $domain1 = $domain2 = "blog.fastrun.cn";

Go文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/7581.html

// 初始化变量
var domain string = "blog.fastrun.cn"
// 批量赋值
var domain,domain1,domain2 string = "blog.fastrun.cn"
// 批量声明赋值
var username,age,local = "zhangsan",13,"BeiJing"
var(
    username="zhangsan"
    age = 13
    local = "BeiJing"
)

常量

PHP文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/7581.html

define("FOO","something");

Go文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/7581.html

// 单独声明
const FOO [string]= something
// 批量声明
const (
    USERNAME = "zhangsan"
    AGE      = 30
)

打印

PHP文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/7581.html

// 基本输出
echo "blog.fastrun.cn";
// 格式化输出
printf("my blog %s","blog.fastrun.cn");

Go文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/7581.html

// 基本输出
fmt.Println("blog.fastrun.cn")
// 格式化输出
fmt.Printf("my blog %s","blog.fastrun.cn")

函数

PHP文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/7581.html

// 基本声明
function printString(string $string){
    echo $string;
}
// 带返回值
function printString(string $string) : string{
    return $string;
}

Go文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/7581.html

// 基本声明
func printString(s string){
    fmt.Println(s)
}
// 带返回值
func printString(s string) string{
    return s
}
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/7581.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/ymba/7581.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定