Go语言之内置函数与包函数

一、内置函数

 Golang中为了编程方便,提供了一些函数,这些函数可以直接使用,称之为Go的内置函数。详情查看:https://studygolang.com/static/pkgdoc/pkg/builtin.htm

如:len、new、make等内置函数。

1、len

用来求长度,比如 string、array等

package main

import "fmt"

func main() {
    // len的使用

    var str string = "hello"
    fmt.Println("str长度", len(str)) // str长度 5
}

2、new

用来分配内存,主要用来分配值的类型,比如int、float系列等,返回的时指针。

package main

import "fmt"

func main() {

    // new的使用
    number := new(int) // 创建一个number变量,类型为*int,值为系统分配的地址
    *number = 10
    fmt.Printf("number的类型为%T,number的值为%v",number,number) // number的类型为*int,number的值为0xc000014098

}

3、make

分配引用类型的内存。

二、包函数

(一)字符串常用函数

 1、len

package main

import "fmt"

func main() {

    var str string = "hello北京" 
    fmt.Println(len(str))

}

2、[]rune()

循环遍历字符串,并且处理中文乱码问题。

package main

import "fmt"

func main() {

    var str string = "hello北京" 
    str2 := []rune(str)
    for i := 0; i < len(str2); i++ {
        fmt.Printf("i=%c\n",str2[i])
    }
    
}
/*
i=h
i=e
i=l
i=l
i=o
i=北
i=京

*/

3、字符串转整数

package main

import (
    "fmt"
    "strconv"
 )

func main() {

    number, error := strconv.Atoi("123")
    if error != nil {
        fmt.Println("error=", error)
    } else {
        fmt.Println("number=", number) // number= 123
    }
    

}

4、整数转字符串

package main

import (
    "fmt"
    "strconv"
 )

func main() {

    str := strconv.Itoa(123)
    fmt.Printf("str值为%v,str类型为%T",str,str) // str值为123,str类型为string
    
}

5、字符串转 []byte

package main

import (
    "fmt"
 )

func main() {

    var bytes = []byte("hello")
    fmt.Printf("bytes=%v", bytes) // bytes=[104 101 108 108 111]
    
}

6、[]byte转字符串

package main

import (
    "fmt"
 )

func main() {

    str := string([]byte{65, 66, 67})
    fmt.Printf("str=%v", str) // str=ABC
    
}

7、十进制转二、八、十六进制

package main

import (
    "fmt"
    "strconv"
 )

func main() {

    str1 := strconv.FormatInt(123, 2)
    fmt.Printf("123对应的二进制为%v\n",str1) // str值为123,str类型为string

    str2 := strconv.FormatInt(123, 16)
    fmt.Printf("123对应的十六进制为%v",str2) // str值为123,str类型为string
    
}
/*
123对应的二进制为1111011
123对应的十六进制为7b
*/

8、查找子串是否在指定的字符串

package main

import (
    "fmt"
    "strings"
 )

func main() {

    str := strings.Contains("abcdef", "abc")
    fmt.Printf("str=%v", str) // str=true
    
}

9、统计一个字符串有几个指定的子串

package main

import (
    "fmt"
    "strings"
 )

func main() {

    num := strings.Count("abcdef", "abc")
    fmt.Printf("num=%v", num) // num=1
    
}

10、不区分大小写的字符串比较

package main

import (
    "fmt"
    "strings"
 )

func main() {

    ifFlag := strings.EqualFold("ABC", "abc")
    fmt.Printf("ifFlag=%v", ifFlag) // ifFlag=true
    
}

11、返回子串在字符串第一次出现的index值,如果没有返回-1

package main

import (
    "fmt"
    "strings"
 )

func main() {

    position := strings.Index("ABA", "A")
    fmt.Printf("position=%v", position) // position=0
    
}

12、返回子串在字符串最后一个出现的index,如果没有就返回-1

package main

import (
    "fmt"
    "strings"
 )

func main() {

    index := strings.LastIndex("ABC", "C")
    fmt.Printf("str index=%v", index) // str index=2
    
}

13、子串替换

将指定的子串替换成另一个子串,strings.Replace(str,"old","new1","new2",n),n指定替换的个数,如果n=-1表示全部替换。

package main

import (
    "fmt"
    "strings"
 )

func main() {

    str := strings.Replace("ABA", "Z", "Z", -1)
    fmt.Printf("str=%v", str) // str=ABA
    
}

14、分割字符串

按照指定的某个字符为分隔符,将一个字符串拆成字符串数组。

package main

import (
    "fmt"
    "strings"
 )

func main() {

    strArr := strings.Split("A,B,C,D", ",")
    fmt.Printf("strArr=%v", strArr) // strArr=[A B C D]
    
}

 15、字符串字母进行大小写转换

package main

import (
    "fmt"
    "strings"
 )

func main() {

    str := "Hello World"
    str1 := strings.ToLower(str)
    str2 := strings.ToUpper(str)
    fmt.Printf("str1=%v\n", str1)
    fmt.Printf("str2=%v", str2)
    
}
/*
str1=hello world
str2=HELLO WORLD
*/

16、将字符串左右两边的空格去掉

package main

import (
    "fmt"
    "strings"
 )

func main() {

    str := strings.TrimSpace(" Hello World ")
    fmt.Printf("str=%v", str) // str=Hello World
    
}
/*
str1=hello world
str2=HELLO WORLD
*/

17、去掉字符串左右两边指定的字符 

package main

import (
    "fmt"
    "strings"
 )

func main() {

    str := strings.Trim("? hello world? ", "?")
    fmt.Printf("str=%q\n", str) // str=" hello world? "
    
}

18、去掉字符串左边指定的字符

package main

import (
    "fmt"
    "strings"
 )

func main() {

    str := strings.TrimLeft("? hello world?", "?")
    fmt.Printf("str=%q\n", str) // str=" hello world?"
    
}

19、去掉字符串右边指定的字符

package main

import (
    "fmt"
    "strings"
 )

func main() {

    str := strings.TrimRight("? hello world?", "?")
    fmt.Printf("str=%q\n", str) // str="? hello world"
    
}

20、判断字符串是否以指定字符串开头

package main

import (
    "fmt"
    "strings"
 )

func main() {

    IsPrefix := strings.HasPrefix("hello world", "hello")
    fmt.Printf("IsPrefix=%v\n", IsPrefix) // IsPrefix=true
    
}

21、判断字符串是否以指定字符串结尾

package main

import (
    "fmt"
    "strings"
 )

func main() {

    IsSuffix := strings.HasSuffix("hello world", "world")
    fmt.Printf("IsSuffix=%v\n", IsSuffix) // IsSuffix=true
    
}

(二)时间与日期相关函数

时间与日期相关函数,需要导入time包。

详情查看 https://studygolang.com/static/pkgdoc/pkg/time.htm

1、time.Time

package main

import (
    "fmt"
    "time"
)

func main() {

    now := time.Now()
    fmt.Printf("now=%v type=%T", now, now) // now=2021-11-21 21:55:00.8398932 +0800 CST m=+0.004958701 type=time.Time

}

2、获取日期其它参数

package main

import (
    "fmt"
    "time"
)

func main() {

    now := time.Now()

    // 通过now获取年月日、时分秒
    fmt.Printf("年=%v\n", now.Year())
    fmt.Printf("月=%v\n", now.Month())
    fmt.Printf("日=%v\n", now.Day())
    fmt.Printf("时=%v\n", now.Hour())
    fmt.Printf("分=%v\n", now.Minute())
    fmt.Printf("秒=%v\n", now.Second())

}
/*
年=2021
月=November
日=21      
时=21
分=58
秒=45
*/

3、格式化时间

  • Printf
  • Sprintf
  • time.Format

Printf:

package main

import (
    "fmt"
    "time"
)

func main() {

    now := time.Now()

    // 通过now获取年月日、时分秒
    fmt.Printf("年月日%d-%d-%d %d:%d:%d", now.Year(),
    now.Month(),now.Day(),now.Hour(),
    now.Minute(),now.Second()) // 年月日2021-11-21 22:4:53

}

Sprintf:

package main

import (
    "fmt"
    "time"
)

func main() {

    now := time.Now()

    // 通过now获取年月日、时分秒
    date := fmt.Sprintf("年月日%d-%d-%d %d:%d:%d", now.Year(),
    now.Month(),now.Day(),now.Hour(),
    now.Minute(),now.Second()) 

    fmt.Printf("date=%v", date) // date=年月日2021-11-21 22:7:43

}

time.Format:

package main

import (
    "fmt"
    "time"
)

func main() {

    now := time.Now()

    // 通过now获取年月日、时分秒
    fmt.Printf(now.Format("2006-01-02 15:04:05")) // 2021-11-21 22:10:11
    fmt.Printf(now.Format("2006-01-02")) // 2021-11-21
    fmt.Printf(now.Format("15:04:05")) // 22:10:11

}

其中“2006/01/02 15:04:05” 这个字符串的数字时固定的,必须这样写。但是字符串的各个数字可以相互组合,因为数字都是唯一的。

4、时间常量

// Duration类型代表两个时间点之间经过的时间,以纳秒为单位。可表示的最长时间段大约290年。
const (
    Nanosecond  Duration = 1
    Microsecond          = 1000 * Nanosecond
    Millisecond          = 1000 * Microsecond
    Second               = 1000 * Millisecond
    Minute               = 60 * Second
    Hour                 = 60 * Minute
)
作者:iveBoy
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须在文章页面给出原文连接,否则保留追究法律责任的权利。
原文地址:https://www.cnblogs.com/shenjianping/p/15583642.html