教程集 www.jiaochengji.com
教程集 >  Golang编程  >  golang教程  >  正文 Golang基础入门01 | 简介

Golang基础入门01 | 简介

发布时间:2022-02-04   编辑:jiaochengji.com
教程集为您提供Golang基础入门01 | 简介等资源,欢迎您收藏本站,我们将为您提供最新的Golang基础入门01 | 简介资源
<h2>介绍</h2>

Go编程语言(也称为Golang)是Google开发的一种开放源代码编程语言,用于创建简单,可靠和高效的软件。 Golang值得学习的原因有很多:

<ul><li>由专家开发</li> <li>语法简单易懂</li> <li>用于Web开发,尤其是后端开发</li> </ul>

在电脑或者您的开发环境中安装Go

Go的安装指南可在此处获得:https://golang.org/dl/

安装完成后,您可以在喜欢的文本编辑器(例如VSCode)中编写Go程序,也可以使用专用的IDE(例如Goland)。

Golang游乐场也可用于学习Go编程语言:https://play.golang.org/

第一个程序

要检查Go是否正确安装在本地计算机中,请输入以下命令。

<pre><code class="lang-go hljs">go version </code></code></pre>

要检查Go环境变量设置,请使用此命令。

<pre><code class="lang-go hljs">go env </code></code></pre>

好了,让我们创建一个名为main.go的文件来编写第一个程序。 根据Go环境设置在Go工作空间目录中创建该文件。

让我们在main.go中编写第一个程序:

<pre><code class="lang-go hljs">package main // 声明包装主 import ( "fmt" // 导入所需的库(例如语法导入“ fmt”包) ) func main() { // 声明程序的主函数 // 从fmt包中,使用名为Println()的函数将输出写入控制台 fmt.Println("Hello, playground") // 开头字母大写表示该函数在包范围内 } </code></code></pre>

要执行该程序,请在用于编写该程序的工作目录中运行此命令。

<pre><code class="lang-go hljs">go run main.go </code></code></pre>

这是输出:

<pre><code class="lang-go hljs">Hello, playground </code></code></pre>

简而言之,执行流程使用自上而下的方法。

<h2>变量</h2>

变量是一种将值存储到特定位置的机制。 使用变量,我们可以存储具有特定类型的任何值,例如字符串,整数甚至数组。

Go具有许多类型的值/数据,可以存储在变量中。 Go数据/值类型的列表为:

布尔值:bool (true / false)

数值类型:int,float64,uint8(其他数值类型)

字符串:string

Array:例如 [4]int {1,2,3,4}

Slice:例如 []int {1,2,3,4}

可以在Go语言规范中检查其他类型:https://golang.org/ref/spec#Types

在数字类型中,有两种主要类型,分别称为无符号(uint8,uint32)和有符号(int,float64)。 unsigned的含义仅是类型中可用的正数。 有符号的int和float64可用于正数和负数。

可以在下面的代码中看到变量声明的示例:

<pre><code class="lang-go hljs">package main import ( "fmt" ) var ( a = 1 b = 2 c = 3 ) //declare multiple variables func main() { data := "Box this lap" //declare variable with shorthand syntax fmt.Println(a, b, c) //print the value of a,b,c fmt.Printf("%T %T %T\n", a, b, c) //print the type of a,b,c variable with %T notation fmt.Println(data) //print the value of data fmt.Printf("%s %T\n", data, data) //print the value with %s notation and the type with %T notation } </code></code></pre>

算术运算有数字类型可用,操作数(算术运算中涉及的变量)必须具有相同的类型。 例如,由于类型不同,无法执行uint类型与int类型之间的操作。

可以在下面的代码中看到算术运算的示例:

<pre><code class="lang-go hljs">package main import ( "fmt" ) var a int = 10 var b int = 2 func main() { d := a b e := a - b f := a / b g := a * b h := a % b //gives the result of remainder between 10 and 2 fmt.Printf("The result of %d %d equals %d\n",a,b,d) fmt.Printf("The result of %d - %d equals %d\n",a,b,e) fmt.Printf("The result of %d / %d equals %d\n",a,b,f) fmt.Printf("The result of %d * %d equals %d\n",a,b,g) fmt.Printf("The result of %d modulo %d equals %d\n",a,b,h) } </code></code></pre> <h2>类型转换</h2>

在Go中,某些类型的值可以转换为另一种类型。 要转换为指定的类型,请使用目标类型,后跟"()"(要从字符串转换为特定类型,请使用strconv.parseType()(示例:strconv.parseInt(" 42",10,64)))

<pre><code class="lang-go hljs">// int -> float64 a := 42 data := float64(a) // string -> int a := "43" data, _ := strconv.Atoi(a) // int -> string a := 43 data := strconv.Itoa(a) </code></code></pre>

要了解有关strconv软件包的更多信息,请查看以下链接:https://golang.org/pkg/strconv/

<h2>资料来源</h2>

以下是一些有用的资源,可进一步了解Go编程语言

https://golang.org/doc/effective_go.html(编写Go代码的最佳做法)

https://golang.org/ref/spec(Go语言规范)

https://godoc.org/(有关Go软件包和第三方软件包的文档)

我希望本文对帮助学习Go编程语言有所帮助。 如果您有任何想法或反馈,可以在下面的评论留言。

关注公众号【技术全沾】学习更多有趣的编程知识。

到此这篇关于“Golang基础入门01 | 简介”的文章就介绍到这了,更多文章或继续浏览下面的相关文章,希望大家以后多多支持JQ教程网!

您可能感兴趣的文章:
Go 零基础编程入门教程
GO语言零基础从入门到精通视频教程
Golang 入门基础教程(一)macOS下安装Golang基础环境
golang基础教程
想系统学习GO语言(Golang
php入门教程(索引)
Go语言 零基础入门到精通项目实战
学习golang开始前的准备工作
适合入门的php基础系列教程
Go语言基础 001

[关闭]
~ ~