PHP To Go 转型手记 (一)

前言

作为一名PHP程序员,我感到荣幸。但在时代不断的变迁中,要具备足够的知识才可生存。

那就从Go语言学起把。

希望看到本篇文章的你可以对Go有一个基本的认识。本系列文章与我自己学习Go语言的方式去描述。以PHP代码与Go代码的对比加以区分理解。

变量

PHP

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

Go

// 初始化变量
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

define("FOO","something");

Go

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

打印

PHP

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

Go

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

函数

PHP

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

Go

// 基本声明
func printString(s string){
    fmt.Println(s)
}
// 带返回值
func printString(s string) string{
    return s
}

致谢

感谢你看到这里,希望本篇文章可以帮到你。谢谢

最后修改:10个月前
如果觉得我的文章对你有用,请随意赞赏

共有 25 条评论

  1. cennik 游客

    Awesome things here. I am very satisfied to see your post.
    Thanks a lot and I’m looking ahead to touch you. Will you kindly drop me
    a mail?

  2. Micah 游客

    hello there and thank you for your info – I’ve definitely picked up something new
    from right here. I did however expertise a few technical points using
    this site, since I experienced to reload the web site lots of times previous to I
    could get it to load correctly. I had been wondering if your web host is OK?
    Not that I am complaining, but sluggish loading instances
    times will often affect your placement in google and could damage your
    quality score if advertising and marketing with Adwords.
    Anyway I am adding this RSS to my email and could look out for much
    more of your respective interesting content. Ensure that you update this
    again very soon.

  3. pieczatki 游客

    Have you ever considered creating an ebook or guest authoring on other
    sites? I have a blog based on the same subjects you discuss and would love to have
    you share some stories/information. I know my subscribers
    would enjoy your work. If you’re even remotely interested, feel free to send
    me an email.

  4. trần thạch cao 游客

    Everyone loves what you guys are usually up too. This
    kind of clever work and coverage! Keep up the good works
    guys I’ve incorporated you guys to my blogroll.

  5. Remco Melis 游客

    Everyone loves it when folks come together and share opinions.

    Great website, continue the good work!

  6. naprawa stron, 游客

    Please let me know if you’re looking for a writer
    for your blog. You have some really good posts and I feel I would be a good asset.
    If you ever want to take some of the load off, I’d love to write some material for your blog in exchange for a link back to
    mine. Please shoot me an e-mail if interested. Thank you!

  7. weselne 游客

    Thanks for some other excellent article. Where else may anybody get that kind of information in such
    an ideal means of writing? I have a presentation next week,
    and I’m on the search for such info.

  8. AGEN JUDI 游客

    Hey there! I just wanted to ask if you ever have any issues with
    hackers? My last blog (wordpress) was hacked and I ended up losing several weeks of hard work due to no back up.
    Do you have any methods to prevent hackers?

    1. crazycodes 管理员

      If you don’t have a backup and the server doesn’t have a snapshot, it’s hard to get it back. I’m sorry

  9. www.kreditrechner-portal.at 游客

    Hmm it appears like your website ate my first comment (it was super long) so I guess I’ll
    just sum it up what I had written and say, I’m thoroughly enjoying your blog.
    I too am an aspiring blog blogger but I’m still new to everything.

    Do you have any recommendations for novice blog writers? I’d certainly appreciate
    it.

  10. agen sv388 游客

    Every weekend i used to go to see this site, as i want enjoyment, for the reason that
    this this web site conations in fact fastidious funny material too.

  11. download 游客

    Wonderful website. A lot of helpful info here. I’m sending
    it to some buddies ans additionally sharing in delicious.
    And obviously, thanks for your sweat!

  12. online dating 游客

    I truly love your site.. Very nice colors & theme. Did you create this web site yourself?
    Please reply back as I’m attempting to create my
    own personal site and would like to find out where you got this from or what the theme is named.
    Thank you!

    1. crazycodes 管理员

      this hankin template , you can do google search

  13. Remco Melis 游客

    I do consider all of the ideas you have offered
    in your post. They’re really convincing and will certainly work.

    Nonetheless, the posts are too brief for beginners. May just you please extend them a bit from subsequent time?
    Thanks for the post.

  14. Remco Melis 游客

    Thanks for ones marvelous posting! I definitely enjoyed reading
    it, you can be a great author.I will be sure to
    bookmark your blog and may come back later in life.

    I want to encourage you to ultimately continue your great writing, have a nice morning!

    1. crazycodes 管理员

      thinks

  15. iphone 7 case kate spade 游客

    I think the admin of this site is in fact working hard in favor of his
    website, since here every information is quality based information.

    1. crazycodes 管理员

      thinks

  16. agen online 游客

    Hey there! Do you use Twitter? I’d like to follow you if that would
    be okay. I’m absolutely enjoying your blog and look forward to new updates.

  17. agen poker 游客

    Thank you for sharing your thoughts. I truly appreciate your efforts and I am
    waiting for your next post thank you once again.

  18. http://www.topadidas.fr 游客

    This piece of writing will help the internet people for creating
    new weblog or even a blog from startt too end.

  19. 匿名 游客

    I enjoy reading a post that can make men and women think.

    Also, thank you for allowing me to comment!

  20. michael kors rhea backpack 游客

    I like the helpful info you provide in your articles.
    I will bookmark your weblog and check again here frequently.
    I am quite certain I will learn many new stuff right here!
    Good luck for the next!

    1. crazycodes 管理员

      thinks