当前位置:网站首页>Basic syntax example for go
Basic syntax example for go
2022-06-13 04:33:00 【gohxc】
Examples of common Syntax
package main // Package name mian A package represents a program that can be executed independently The package name can be different from the directory name One package per directory
import (
"fmt"
"os"
)
//init Special functions , Every package containing this function will execute this function first
func init(){
}
// Main executive function
func main() {
fmt.Println("hello, world")
}
// Basic types int、float、bool、string
// The compound type struct ( Structure )、array、slice( section )、map、channel( passageway )
// Interface interface
// function
func add(n1 int,n2 int)(num int){
num= n1+n2
return n1+n2
}
const Pi = 3.14159
var str string // The default value is int by 0,float by 0.0,bool by false,string Is an empty string , Pointer for nil
type TZ int32 //TZ Express int32 Another name for
time.Now() // current time
if condition1 {
// do something
} else if condition2 {
// do something else
}else {
// catch-all or default
}
switch var1 {
case val1:
...
case val2,val3,val4:
case 0: // Empty branch , Only when i == 0 Only when you enter the branch
case 0: fallthrough // The code that takes the next branch
...
default:
...
}
for i := 0; i < 5; i++ {
fmt.Printf("This is the %d iteration\n", i)
}
for pos, char := range str {
...
}
// Structure With label
type TagType struct { // tags
field1 bool "An important answer"
field2 string "The name of the thing"
field3 int "How much there are"
}
Common methods
// character string
- character string str Of the 1 Bytes :str[0]
- The first i Bytes :str[i - 1]
- Last 1 Bytes :str[len(str)-1]
Use the plus sign + Splicing strings is not the most efficient way , A better way is to use functions strings.Join()
strings and strconv package
HasPrefix Judgment string s Whether or not to prefix start :
strings.HasPrefix(s, prefix string) bool
Contains Judgment string s Does it include substr:
strings.Contains(s, substr string) bool
Index Return string str In string s Index in (str Index of the first character of ),-1 Representation string s Does not contain a string str:
strings.Index(s, str string) int
Replace Used to string str In front of n A string old Replace with string new, And return a new string , If n = -1 Replace all strings old For the string new:
strings.Replace(str, old, new, n) string
strings.ToLower(s) string // Replace with corresponding lowercase characters
strings.TrimSpace(s)// To eliminate the white space at the beginning and end of the string
strings.Split(s, sep) Used to customize the split symbol to split the specified string , Also return to slice.
Join Used to set the element type to string Of slice Use split symbols to splice together to form a string :
strings.Join(sl []string, sep string) string边栏推荐
- 120. triangle minimum path sum - Dynamic Planning
- Redis
- 第三方评论插件
- 剑指 Offer 11. 旋转数组的最小数字-二分查找
- Idea Download
- Mongodb compass connects to the Alibaba cloud remote server database or reports an error occurred while loading instance info: command hostinfo req
- Get verification code
- 【Try to Hack】upload-labs通关(暂时写到12关)
- 2022 ICLR | CONTRASTIVE LEARNING OF IMAGE- AND STRUCTURE BASED REPRESENTATIONS IN DRUG DISCOVERY
- [flutter problem Series Chapter 67] the Solution to the problem of Routing cannot be jumped again in in dialog popup Using get plug - in in flutter
猜你喜欢

2022 ICML | Pocket2Mol: Efficient Molecular Sampling Based on 3D Protein Pockets

ACM ICPC

基于DE2-115平台的VGA显示

电磁兼容常用名词术语
![[flutter problem Series Chapter 67] the Solution to the problem of Routing cannot be jumped again in in dialog popup Using get plug - in in flutter](/img/59/0d95619ee3bba1f8992d90267d45c2.png)
[flutter problem Series Chapter 67] the Solution to the problem of Routing cannot be jumped again in in dialog popup Using get plug - in in flutter

Sword finger offer 56 - I. number of occurrences in the array

Uni app Ali font icon does not display

VGA display based on de2-115 platform

Redis data persistence

The could not find com scwang. smart:refresh-layout-kernel:2.0.3. Required by: project: the app cannot load the third-party package
随机推荐
ACM ICPC
Redis master-slave replication, sentinel mode, cluster
MySQL index
剑指 Offer 56 - I. 数组中数字出现的次数
Introduction to applet Basics (dark horse learning notes)
Notes on software test for programmers -- basic knowledge of software development, operation and maintenance
[chapter 67 of the flutter problem series] the solution to the problem that the get plug-in cannot jump to the route twice in the dialog pop-up window in flutter
PowerShell:因为在此系统上禁止运行脚本,解决方法
February 25, 2021 (Archaeology 12 year Landbridge provincial competition)
Lenovo notebook computer uses the insert key. When the mouse becomes a small square, how to solve it
Applet waterfall flow
Collection of wrong questions in soft test -- morning questions in the first half of 2010
CreateAnonymousThreadX给匿名线程传递参数
Get parameters on link
web自动化测试之webdriver api总结
Implementation of homepage header function in PHP development blog system
用navicat将mysql数据转化为postgresql
Collection of wrong questions in soft test -- morning questions in the first half of 2011
Serial communication learning
php开发14 友情链接模块的编写