go 学习笔记 hello world

下面来go官方文档
The Go Project
Go is an open source project developed by a team at Google and many contributors from the open source community.
Go is distributed under a BSD-style license.

The Go Programming Language
The Go programming language is an open source project to make programmers more productive.
Go is expressive, concise, clean, and efficient. Its concurrency mechanisms make it easy to write programs that get the most out of multicore and networked machines, while its novel type system enables flexible and modular program construction. Go compiles quickly to machine code yet has the convenience of garbage collection and the power of run-time reflection. It’s a fast, statically typed, compiled language that feels like a dynamically typed, interpreted language.

第一个go程序

package main                     // required
import "fmt"                     // for Println, 可以暂时理解为C中的 #include <stdio.h>

func main() {                    // go 程序入口,注意:左大括号的位置
	fmt.Println("hello world")   // 打印消息并换行,类似java中的System.out.println
}

执行这段代码,将在屏幕上输出 hello world

下面是实现相同功能的C语言版本

#include <stdio.h>

int main()
{
	printf("hello world\n");
	
	return 0;
}

可以明显观察到二者有诸多不同的地方:

  1. go 中,main 所在文件必须使用 package main 来定义一个名为 main 的包,表示这是一个可独立执行的程序
  2. go 使用类似 java 或 python 的 import 关键字,来引入所需要使用的包或包的元素
  3. go 使用 func 关键字标识函数
  4. go 也使用 “{” 和 “}” 来标识语句块,且通常禁止 “{” 出现在行首(当然也有例外,如下)
  5. go 的 main 函数没有返回值
  6. 如果 main 所在文件中包含 init 函数,则在 main 前会先执行 init 函数
  7. go 与 C/C++ 使用同样的注释语法
func foo() {
	{ 	/* 这个时候,"{" 是可以单独出现在某一行的,它即不是函数体开始的标识,也不是流程控制结构或循环结构开始的标识 */
		...
	}
}

总结:
英文不好,不翻译了
go 使用 package 创建包,每个源文件必包含在某包中,main函数所在源文件必须在名为 main 的包中,且程序从 main 包开始运行
go 使用 import 引入包或包的元素,来使用相关的功能
go 使用 func 声明函数
go 使用 “{” 和 “}” 来标识语句块,且通常禁止 “{” 出现在行首
go 的入口为 main 函数;
go 的注释与 C/C++ 具有同样的格式
go 的 main 函数没有返回值

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值