Getting started with Go language - process control

Keywords: Go less Unix

go process control

String explanation

String principle

  • The bottom layer of string is a byte array, so it can be converted to and from [] byte type
  • Characters in a string cannot be modified
  • String is composed of byte, so the length of string is the length of byte
  • The run type (int32 in four bytes) is used to represent utf8 characters. A run consists of one or more bytes
func stringByte() {
   var str string
   str = "abc Hello"
   var b []byte = []byte(str)
   var c[]rune = []rune(str)
   fmt.Printf("%c\n",97)
   fmt.Printf("b=%v,\tlen(str)=%d\n",b,len(str))
   fmt.Printf("%d\n",len(c))
}

Practice

Write a program to reverse the English string

func reverseStr(s string) string {
   bytes := []byte(s)
   for i:=0;i<len(s)/2;i++{
      //FMT. Printf (""% C ", I) output string
      var tmp = bytes[i]
      bytes[i] = bytes[len(s)-i-1]
      bytes[len(s)-i-1] = tmp
   }
   s = string(bytes)
   return s
}

func main() {
   //stringByte()
   s:= "abcdefg"
   fmt.Println(reverseStr(s))
}

Write a program to reverse the string containing Chinese

func reverseChinese(s string)  {
   bytes := []rune(s)
   for i:=0;i< len(bytes)/2;i++ {
      bytes[i],bytes[len(bytes)-i-1] = bytes[len(bytes)-i-1],bytes[i]
   }
   s = string(bytes)
   fmt.Println(s)
}

func main() {
   s:= "abcdefg China"
   fmt.Println(reverseChinese(s))
}

Write a program to determine whether a string is palindrome

func isReversed(s string) string {
   r := []rune(s)
   for i:=0;i <len(r)/2;i++ {
      r[i],r[len(r)-i-1] = r[len(r)-i-1],r[i]
   }
   s1 := string(r)
   if s1 == s{
      return "is huiwen"
   }else {
      return "not huiwen"
   }
}

Date and time type

  • time package
  • Get current time
  • Time format
  • Conversion between time stamp and time
  • Simple use of timer
  • time.Duration() for nanoseconds
package main

import (
   "fmt"
   "time"
)

func getTime() {
   now:=time.Now()
   year := now.Year()
   month := now.Month()
   day := now.Day()
   hour := now.Hour()
   minute := now.Minute()
   second := now.Second()
   fmt.Printf("%v\n",now)
   fmt.Printf("year=%d,month=%d,day=%d,hour=%d,minute=%d,second=%d\n",
      year,month,day,hour,minute,second)
}

func getTimeStamp() {
   now := time.Now()
   timestamp := now.UnixNano()
   fmt.Printf("%d\n",timestamp)
}

func Dingshiqi() {
   timer :=time.NewTimer(time.Second)
   for v := range timer.C {
      fmt.Printf("time:%v\n",v)
      //Refresh in one second
      timer.Reset(time.Second)
   }
}

func TimeTicker() {
   timer := time.NewTicker(time.Second)
   for v := range timer.C {
      fmt.Printf("time: %v\n",v)
   }
}

func TimeStampToTime(timestamp int64) {
   time_t := time.Unix(timestamp,0)
   fmt.Printf("time %v\n",time_t)
}

func FormatTime() {
   now := time.Now()
   //Go birth time
   str := now.Format("2006/01/02 15:04:05")
   fmt.Printf("str: %s\n",str)
}

func main() {
   //getTime()
   //getTimeStamp()
   /*
   //go Dingshiqi()
   go TimeTicker()
   time.Sleep(time.Minute)
    */
   //TimeStampToTime(time.Now().Unix())

}

Small exercise

Write a program, count the execution time of a code, and the unit is accurate to microseconds

func testCase() {
   for i:=0;i<10000000000;i++{
      _=i
   }
}

func main() {
   //Get nanoseconds
   start_time := time.Now().UnixNano()
   testCase()
   end_time := time.Now().UnixNano()
   fmt.Printf("cost time is %d us,also %d ms\n",(end_time-start_time)/1000,(end_time-start_time)/1000000)

}

Process control

if conditional statement

func judgeNum() {
   num := 10
   if num % 2 ==0 {
      fmt.Printf("%d Even numbers\n",num)
   }else {
      fmt.Printf("%d It's odd.\n",num)
   }
}
func guessNum() {
   num := 99
   if num <=50 {
      fmt.Printf("%d Up to 50\n",num)
   } else if num >= 51 && num <= 100 {
      fmt.Printf("%d 51 or more, 100 or less\n",num)
   } else {
      fmt.Printf("%d Greater than 100\n",num)
   }
}

for cycle

func simpleFor() {
   for i:=1;i<=10;i++ {
      fmt.Printf("%d\n",i)
   }
}

func forcase() {
   i := 0
   for ;i<=10; {
      fmt.Printf("%d\n",i)
      i+=2
   }
}

func breakFor() {
   for i:=0;i<=10;i++ {
      if i > 5 {
         break
      }
      fmt.Printf("%d\n",i)
   }
}

func continueFor() {
   for i:=0;i<=10;i++ {
      if i == 5 {
         continue
      }
      fmt.Printf("%d\n",i)
   }
}

func forCase() {
   for no,i := 10,1;i<=10&&no<=19;i,no=i+1,no+1 {
      fmt.Printf("%d * %d = %d\n",no,i,no*i)
   }
}

// Infinite cycle
func deadLoop() {
   for {
      fmt.Printf("hello")
   }
}

switch Statements

func switchCase() {
   f:=9
   switch f {
   case 1:
      fmt.Printf("enter case 1\n")
      fmt.Printf("f=1\n")
      //fallthrough
   case 2:
      fmt.Printf("enter case 2\n")
      fmt.Printf("f=2\n")
   case 3:
      fmt.Printf("enter case 3\n")
      fmt.Printf("f=3\n")
   default:
      fmt.Printf("enter default case\n")
   }
}

func switchCase2 () {
   letter := "i"
   switch letter {
   case "a","e","i","o","u":
      fmt.Printf("Vowel%s\n",letter)
   default:
      fmt.Printf("Consonant letters%s\n",letter)
   }
}

//Conditional judgment format
func switchCase3() {
   num := 75
   switch  {
   case num<75:
      fmt.Printf("number %d Less than 75\n",num)
   case num>=75 && num <=85:
      fmt.Printf("number%d More than 75, less than 85\n",num)
   default:
      fmt.Printf("number%d Greater than 85\n",num)
   }
}

Small exercise

Number guessing exercise

func randCase() {
   var number int
   /*
   Pseudorandom number
   for i:=0;i<10;i++{
      number = rand.Intn(100)
      fmt.Printf("number:%d\n",number)
   }
   return
    */
   //Generate random number seed
   rand.Seed(time.Now().UnixNano())
   number = rand.Intn(100)
   fmt.Println(number)

   fmt.Printf("Guess a number,Numbers range from 0 to 100\n")
   for {
      var input int
      fmt.Scanf("%d\n",&input)
      var flag bool = false
      switch  {
      case number > input:
         fmt.Printf("The input is relatively small%d\n",input)
      case number == input:
         fmt.Printf("Guess right.%d,The correct value is%d\n",input,number)
         flag = true
      case number<input:
         fmt.Printf("The input is relatively large%d\n",input)
      }
      if flag {
         break
      }
   }
}

Posted by fredted40x on Sat, 14 Mar 2020 08:37:41 -0700