当前位置:网站首页>Go language learning notes (1)
Go language learning notes (1)
2022-07-27 15:34:00 【Gold 250】
hello , world
package main
import (
"fmt"
)
func main{
fmt.Println("hello, world")
}
go The structure of language :
- package , import, function
- Package concept and Java In the same way
- import Is equivalent to C Medium include
- The concept of function is the same as that in other languages
Requirements of braces
- go The program has mandatory requirements for the position of braces , You must write left parentheses on the same line ( When you need to write braces )
If the above hello world The program is written as follows , Will be an error , Be careful main Brace position of function
package main
import (
"fmt"
)
func main
{
fmt.Println("hello, world")
}
although go There is no need to write a semicolon at the end of every sentence , But in fact, the compiler will add a semicolon to every sentence before compiling , If the braces are not written according to the format requirements , It will cause the compiler to be unrecognized and report syntax errors
Variable declarations
package main
import (
"fmt"
)
func main
{
const // declare constant
var // Declare variables
Variable name := expression // Declare local variables with smaller scope
}
- When used as a function local variable , var And := The effect of the declaration is the same
- If you need to declare it as a cyclic variable, the effect is different , var The declaration can survive until the end of the function , := The declaration method can only survive until the end of the cycle
Judgment and circulation
if, else if, else
package main
import (
"fmt"
)
func main{
if 1 >= 2 {
fmt.Println("a")
} else if 1 >= 3 {
fmt.Println("b")
} else {
fmt.Println("c")
}
}
- The judgment conditions are similar Python In the form of , There is no need for brackets , C Parentheses are mandatory in language
- If there are many conditions , You can use parentheses to prevent logical confusion
go There are only for loop
There are scope constraints that can be done
package main
import (
"fmt"
)
func main{
for i := 1; i < 10; i++ {
// The effect in uplink is equivalent to C In language for (int i = 1; i < 10; i++)
// This is the circulatory body
}
}
If in for Don't write the cycle condition next , It will become an endless cycle , Here's the picture
package main
import (
"fmt"
)
func main{
// It's a dead cycle
for {
// This is the circulatory body
}
}
Realization while loop
- Add the condition of jumping out of the loop in the loop body of the dead loop
- The sentence that jumps out still uses break
go Medium break and continue The function and usage of sentences are the same as those of other languages
switch sentence
And C The language is very similar
package main
import (
"fmt"
)
func main{
switch expression {
case : {
}
case : {
}
}
}
Function declaration
package main
import (
"fmt"
)
func Function name ( The name of the parameter 1 parameter types 1, The name of the parameter 2, parameter types 2) Function return value type {
// The body of the function
return
}
func main{
}
- If there are multiple formal parameters , Then follow the above format and so on
- go The language allows multiple function return values
- If there are multiple function return values, you need to write the return value type in parentheses
import
- Reference resources Python Medium import The form can be
- The library name needs to be caused by double quotation marks
- If only import A library may not use parentheses
- import The library contains multiple times , It needs to be enclosed in parentheses
Static type language
- go Language belongs to static type language
- After each variable is declared, the type cannot be changed , The content can be changed
- C, Java It's a dynamically typed language
- The characteristics of static types can help compilers perform compilation work faster , Increase of efficiency
Keep specific decimal output
- fmt.Printf() Use this
- Refer to C In language printf usage , Mostly the same , Formatting flags are partly different , Just consult the reference manual
big package
- Used to solve high-precision calculation
- The underlying principle is to perform large number operations based on strings , Refer to high-precision algorithm
边栏推荐
猜你喜欢

Leetcode 240. search two-dimensional matrix II medium

Method of removing top navigation bar in Huawei Hongmeng simulator

IJCAI 2022 outstanding papers were published, and 298 Chinese mainland authors won the first place in two items

generic paradigm
Principle of MOS tube to prevent reverse connection of power supply
仪表放大器和运算放大器优缺点对比

Spark 任务Task调度异常分析

USB2.0接口的EMC设计方案

Leetcode 783. binary search tree node minimum distance tree /easy

Selenium 报错:session not created: This version of ChromeDriver only supports Chrome version 81
随机推荐
QT (five) meta object properties
MLX90640 红外热成像仪测温传感器模块开发笔记(七)
光电隔离电路设计方案(六款基于光耦、AD210AN的光电隔离电路图)
Photoelectric isolation circuit design scheme (six photoelectric isolation circuit diagrams based on optocoupler and ad210an)
Leetcode 781. rabbit hash table in forest / mathematical problem medium
EMC design scheme of RS485 interface
js运用扩展操作符(…)简化代码,简化数组合并
Selenium reports an error: session not created: this version of chromedriver only supports chrome version 81
Network equipment hard core technology insider router Chapter 18 dpdk and its prequel (III)
Sword finger offer merges two sorted linked lists
Network equipment hard core technology insider router Chapter 4 Jia Baoyu sleepwalking in Taixu Fantasy (Part 2)
Spark Bucket Table Join
积分运算电路的设计方法详细介绍
Discussion on STM32 power down reset PDR
Leetcode 341. flattened nested list iterator DFS, stack / medium
DevEco Studio2.1运行项目报错
Is it safe to open an account on a mobile phone?
Network equipment hard core technology insider router Chapter 6 tompkinson roaming the online world (middle)
Principle of MOS tube to prevent reverse connection of power supply
Leetcode 456.132 mode monotone stack /medium