Go实战--go中一些有用的代码片段(The way to go)
生命不止,继续 go go go !!! 今天跟大家分享一些有用的,简单的golang代码片段。 改变string的值 str := "hello"
str[0] = 'c'
编译错误:cannot assign to str[0] 正确的做法: package main
import (
"fmt"
)
func main() {
str := "hello"
c := []byte(str)
c[0] = 'f'
str2 := string(c)
fmt.Println(str2)
}
输出:fello 获得子字符串 package main
import "fmt"
func main() {
value := "cat;dog"
// Take substring from index 4 to length of string.
substring := value[4:len(value)]
fmt.Println(substring)
}
输出:dog 遍历string输出 package main
import "fmt"
func main() {
value := "cat;dog"
for ch := range value {
fmt.Println(ch)
}
}
输出: package main
import "fmt"
func main() {
value := "cat;dog"
for _,ch := range value {
fmt.Println(ch)
}
}
输出: 但是,这还不是你想要的,所以: package main
import "fmt"
func main() {
value := "cat;dog"
for _,ch := range value {
fmt.Println(string(ch))
}
}
字符串中bytes和characters package main
import (
"fmt"
"unicode/utf8"
)
func main() {
value := "爱情"
i := utf8.RuneCountInString(value)
fmt.Println(i)
j := len(value)
fmt.Println(j)
}
输出: 在二维数组或切片中查询某个值 package main
import (
"fmt"
)
func main() {
v := 11
arr2Dim := [2][2]int{}
arr2Dim[0][0] = 1
arr2Dim[0][1] = 11
arr2Dim[1][0] = 111
arr2Dim[1][1] = 1111
found := false
Found:
for row := range arr2Dim {
for column := range arr2Dim[row] {
if arr2Dim[row][column] == v {
found = true
break Found
}
}
}
fmt.Println(found)
}
输出:true 检查map中是否存在某个key package main
import (
"fmt"
)
func main() {
map1 := map[string]int{"one": 1,"two": 2}
val1,isPresent := map1["one"]
fmt.Println(val1)
fmt.Println(isPresent)
val2,isPresent2 := map1["three"]
fmt.Println(val2)
fmt.Println(isPresent2)
}
输出: 检测某个值是否实现了某一接口 package main
import (
. "launchpad.net/gocheck"
"testing"
)
func Test(t *testing.T) {
TestingT(t)
}
type MySuite struct{}
var _ = Suite(&MySuite{})
type IFoobar interface {
foobar()
}
type Foobar struct {
}
func (f *Foobar) foobar() {
}
func (s *MySuite) TestFoobar(c *C) {
v := Foobar{}
var i interface{} = v
_,ok := i.(IFoobar)
c.Assert(ok,Equals,false)
var p interface{} = &v
_,ok = p.(IFoobar)
c.Assert(ok,true)
}
打开并读取文件 package main
import (
"io"
"os"
)
func main() {
// open input file
fi,err := os.Open("input.txt")
if err != nil {
panic(err)
}
// close fi on exit and check for its returned error
defer func() {
if err := fi.Close(); err != nil {
panic(err)
}
}()
// open output file
fo,err := os.Create("output.txt")
if err != nil {
panic(err)
}
// close fo on exit and check for its returned error
defer func() {
if err := fo.Close(); err != nil {
panic(err)
}
}()
// make a buffer to keep chunks that are read
buf := make([]byte, 1024)
for {
// read a chunk
n,err := fi.Read(buf)
if err != nil && err != io.EOF {
panic(err)
}
if n == 0 {
break
}
// write a chunk
if _,err := fo.Write(buf[:n]); err != nil {
panic(err)
}
}
}
使用outil.ReadFile package main
import (
"io/ioutil"
)
func main() {
// read the whole file at once
b,err := ioutil.ReadFile("input.txt")
if err != nil {
panic(err)
}
// write the whole body at once
err = ioutil.WriteFile("output.txt",b, 0644)
if err != nil {
panic(err)
}
}
检测某个channel是否被关闭 if input,open := <-ch; !open {
break
}
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |