首页 > 编程语言 >go语言实现第一个程序-hello,world!

go语言实现第一个程序-hello,world!

时间:2022-11-16 14:32:50浏览次数:73  
标签:packages run tool Go go world main hello


0.前言
工作中一直使用c++编写高并发服务器程序,但c++编写高并非服务器程序时多线程逻辑,锁机制导致的死锁,内存泄漏,崩溃等问题会耗费大量时间和精力。

听同事说go语言是专门做高并发编程的,不用考虑上面的一些问题,由google推出。想想google出品,必属精品,又和自己的工作方向相关,所以了解一下。

1.编译工具安装

使用源码安装或者命令安装都可以,ubuntu下使用命令安装:

$ sudo apt-get install golang


安装完成后,看看版本号:

$ go version
go version go1.6.2 linux/amd64

在shell终端输入go命令看看用法:

$ go
Go is a tool for managing Go source code.

Usage:

go command [arguments]

The commands are:

build compile packages and dependencies
clean remove object files
doc show documentation for package or symbol
env print Go environment information
fix run go tool fix on packages
fmt run gofmt on package sources
generate generate Go files by processing source
get download and install packages and dependencies
install compile and install packages and dependencies
list list packages
run compile and run Go program
test test packages
tool run specified go tool
version print Go version
vet run go tool vet on packages

Use "go help [command]" for more information about a command.

Additional help topics:

c calling between Go and C
buildmode description of build modes
filetype file types
gopath GOPATH environment variable
environment environment variables
importpath import path syntax
packages description of package lists
testflag description of testing flags
testfunc description of testing functions

Use "go help [topic]" for more information about that topic.

里面常用的就是build和run命令了。

2.编译运行命令

go语言的源码后缀名为.go,写一个main.go的hello world程序:

// main.go
package main

import "fmt"

func main() {
fmt.Println("Hello, World!")
}


注释支持c++的注释方式,需要注意的一点就是函数的大括号必须放在后面,否则编译不通过。


编译命令:

go build -o main main.go

编译后就生成了执行程序main,在终端下./main可以直接运行了


编译运行命令:

$ go run main.go
Hello, World!


3.文档以及参考资料

​go语言教程 | 菜鸟教程​

​go语言中文社区​

标签:packages,run,tool,Go,go,world,main,hello
From: https://blog.51cto.com/u_4296776/5856289

相关文章

  • Go语言中字符串的查找方法小结
    这篇文章主要介绍了Go语言中字符串的查找方法小结,示例的main函数都是导入strings包然后使用其中的方法,需要的朋友可以参考下 1.funcContains(s,substrstring)bool这......
  • 1.django简介及安装
    1.简介web应用程序的本质接收并解析HTTP请求处理本次请求,完成业务上的处理构造并返回响应HTTP响应   什么是web框架呢?web框架用于搭建web应用程序,免......
  • 使用googlecode+TortoiseSVN进行版本控制(转)
    1.简介       随着写代码越来越多,做的项目越来越多,我们时常会感到反复修改调试代码是个很费劲的过程,有时写好了一段代码,可以工作了,在此基础上再做修改,发现不能工作......
  • 随记:pywebio+django报错“Failed to load resource”,cdn失效
    自己用Django+pywebio搭建的网站一直正常运转,但近期出现了报错“Failedtoloadresource”,链接显示:“http://127.0.0.1:8000/?_pywebio_cdn=False”,经查阅官网资料,应该......
  • 【Django】RBAC权限管理系统模块-理解
    今天文章分为两部分:)PART1 RBAC权限管理内容分享/ PART2关于字节跳动一面  10Minutes Django-RBAC:PART1 这权限管理系统主要功能是什么?顾名思义,在系统中可以灵......
  • MongoDB - 增删改查
    连接标准URI连接语法通常,可以设定标准的URI连接语法,作为连接配置:mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][......
  • 3 django 配置mysql数据库
    1.MYSQL安装https://blog.csdn.net/qq_59636442/article/details/123058454数据库密码:123456端口号和用户名输入密码,点击check......
  • golang 进度条
    packagemainimport( "fmt" "strings" "time")funcmain(){ str:="#" str=strings.Repeat(str,50) i:=0 fori<=100{ s:=str[:i/2] s1:=......
  • MongoDB配置文件详解
    一配置文件说明MongoDB有两种配置文件格式,分别是:3.2版官方yaml配置文件选项参考用=号的常规格式类似my.conf等常规配置的文件yaml语法的新格式mongodb3.x版本后就......
  • golang - 1.19版本 recover 的使用
    panic的使用参考我这个随笔https://www.cnblogs.com/c2g5201314/p/16894630.html如果需要当前函数中断,父级调用的函数不断,可使用recover在1.19版本开始需要这样写......