Part of the spring of 22

Bamboo cloud technology has one, two and three aspects:

What are the potential safety hazards of the project (I see that I have more safety development, which extends to) enterprises or services? (I answered external hacker attacks, loopholes and social workers' disclosure; it is concluded that the internal hidden dangers are greater) - "what are the security protection measures? -" (the interviewer pointed out that prevention is more important, leading to) zero trust system (my job is IAM)

Go: channel; GPM; Slice expansion; What else has the sync package used? (map, waitgroup, mutex, etc.)

Go channel implementation principle analysis - brief book

Deeply understand Go's slice and its expansion rules | QXQZX's Blog
GoLang GPM model - Go language Chinese network - golang Chinese community

Go language sync package application details - Zhihu

mysql: SQL optimization

During table building: try to use vchar replace char,Index the frequently queried and sorted fields, and try to use composite index
 In the query process: try to avoid index failure, avoid full table query and avoid select * ,Leftmost rule in composite index

Redis: data type, how to ensure high availability? (cluster, master-slave read-write separation + synchronization, sentry, memory elimination)

https specific process (ca, certificate chain verification process, asymmetric encryption exchange, random KEY, symmetric encryption data), ssl algorithm

The most thorough HTTPS in the whole network (often asked in interview)

Write a producer consumer model. The producer generates 100 pieces of data each time, and the consumer reads 10 pieces each time:

package  main

import (
	"fmt"
	"sync"
)

func main(){
	var mychan  =make(chan int,100)
	var mymutex = sync.Mutex{}
	var wg sync.WaitGroup
	wg.Add(10)
	var p Producer
	var w Worker

	go p.Produce(&mychan,&mymutex)
	var res []int
	for i:=0;i<10;i++{
		go w.Consume(&mychan,&wg,&res)
	}
	wg.Wait()
	close(mychan)

}


type Producer struct {

}

type Worker struct{

}

func (p *Producer)Produce(mychan *chan int,mymutex *sync.Mutex){
	mymutex.Lock()
	for i:=0;i<100;i++{
		*mychan <-i
	}
	mymutex.Unlock()
}

func (w *Worker)Consume(mychan *chan int,wg *sync.WaitGroup,res *[]int){
	for i:=0;i<10;i++{
		data := <-*mychan
		fmt.Println(data)
		*res = append(*res,data)
	}
	wg.Done()
}

Line sensitive side:

Go: GPM model, the difference between new and make, lock,

mysql: lock, transaction

Problems encountered in the project, cycle

Career planning.. (I guess it's a slip of tongue here. The interviewer finally asked if he had any plans to return to Xi'an. I said it for a few years, and then it was cut off.)

Telyingfu side:

Go: the difference between make and new; Memory leakage (the phenomenon is that the memory is being consumed by a process, which is often encountered in c. the memory applied by malloc should be released with free, otherwise memory leakage will occur, and garbage collection will be used in go); init execution sequence (before the main function, those in the module package will be executed first, and those in the package main will be executed later); The difference between buffered and unbuffered channels (in a word, buffered channel reads and writes are asynchronous and unbuffered reads and writes are synchronous)

How many times do the hour and minute hands meet in a day

redis: persistent, how to recover after data loss

Tools: how git fallback; How to compile docker (specific to the command in dockerfile)

mysql: index; leftmost prefixing

python: cls decorator; Concurrent

Two sides:

redis what to do if the memory is full (you should answer the memory elimination strategy)

What if rmq consumers hang up and how to ensure that only one consumer gets the task

(I didn't answer the scene questions well. I haven't had such high concurrency experience. I'm also a blockchain worker. I don't talk about speculation.)

Three sides:

Ha ha, ha ha, let me talk about the seven layer model. I'm so angry that I haven't seen it for too long. Which layer is the TCP/IP protocol on? What protocol does the application layer have? That's great. Finally someone can explain the TCP/IP protocol clearly- 51CTO.COM

How to ensure the safety of docker? For example, copy configuration files are often required in docker. There is a risk of data leakage if stored in plaintext. Encrypting sensitive data and decrypting it in the program can prevent data leakage

Didi interview:

Self introduction - what are the difficulties encountered in safety research and development?

Talk about the project - sso single sign on process?

What is single sign on (SSO)

mysql: index, non clustered and clustered? (the primary key index is clustered, and the rest are non clustered) what specific scenarios use what locks? How to ensure the consistency between master and slave? How to rollback?

MySql master-slave replication, from principle to practice!

What is the data structure of red is: string? (sds) memory elimination mechanism? (scheduled deletion, inert deletion)

go: channel details, is map slice concurrent security? Why, mutex, wait group

Network IO: epoll

Did a problem: use the sliding window algorithm (pay attention to the boundary)

package main

import "fmt"
/*
The minimum length of continuous subarray of positive integer nums = target
 */
func minSubArrayLen(target int, nums []int) int {
	var length = len(nums)
	if length ==0{
		return 0
	}
	var minlength = length
	var sum = 0
	for start,end:=0,0;end<=length && start <= end;{
		if sum >= target{
			tempMinLen := end-start
			if  tempMinLen < minlength{
				minlength = tempMinLen
			}
			sum -= nums[start]
			start++
		}else{
			if end < length{
				sum += nums[end]
			}
			end ++
		}
	}
	return minlength
}

func testMinSunArr(){
	res := minSubArrayLen(7,[]int{2,3,1,2,4,3})
	fmt.Println(res)

	res1 := minSubArrayLen(4,[]int{1,4,4})
	fmt.Println(res1)
}

func main(){
	testMinSunArr()
}

Mysql index interview questions - meat cutter - blog Garden s

The blue army is convinced to attack and defend

go: GPM

mysql: the request volume is too large. How to tune it? (1 sub database and sub table 2 use cluster, and say master-slave synchronization 3 use redis cache hotkey)

redis: is it a single thread? (network IO and kv read / write are single thread) what if an avalanche happens?

Network IO: mail protocol, epoll model, whether the packet has been captured, Linux packet capture command

Can you crawl

What problems do you encounter when talking about fishing

Two sides:

Go: bottom layer of channel. How to read and write data

Network IO model, select epoll

Project, sso

Do you usually learn from the source, encapsulate the underlying data structure, and understand the vulnerability and mining technology?

Deeply convinced of three aspects:

Why did you leave. Career planning. Overtime attitude.

Chengmai technology side:

Talking about projects, why choose Rocket MQ

Data structure at the bottom of ordered set in redis (jump table plus hash)

Mysql writes SQL statements and checks the number of male and female students in each grade in the student table; How to do distributed synchronization? (if a transaction requires multiple application services to complete the write operation, and one of them fails, the previous transaction needs to be rolled back. By the way, the principle of rollback is also mentioned.)

Go:GPM ; The difference between slice and array, and how slice can be expanded; What are the effects of reading and writing data to closed channel s? (write back to Panic, read back to zero); How to catch exceptions (defer and recover)

Algorithm: step jumping problem, multiple concurrent processes are executed concurrently, how to ensure sequential printing?

package main

import (
	"fmt"
	"sync"
)

func main(){
	var (
		Awg sync.WaitGroup
		Bwg sync.WaitGroup
		Cwg sync.WaitGroup
	)
	for i:=0;i<10;i++{
		Awg.Add(1)
		Bwg.Add(1)
		Cwg.Add(1)
		go func() {
			fmt.Print("A")
			Awg.Done()
		}()

		go func() {
			Awg.Wait()
			fmt.Print("B")
			Bwg.Done()
		}()

		go func() {
			Bwg.Wait()
			fmt.Print("C")
			Cwg.Done()
		}()

		Cwg.Wait()
		fmt.Println()
	}
}

Tiktok side:

Talk about projects, responsibilities and difficulties

redis set zset underlying data structure

mysql transaction, isolation level, mvcc, how to realize the readability, submission and readability

How to ensure the reliability of TCP and the specific process of congestion control

go's garbage collection

I did a problem (I didn't write it out, so my mind was fixed

package main

import "fmt"

func main() {
	var matrix = [][]int{[]int{1,2,3,10},[]int{4,5,6,11},[]int{7,8,9,12}}
	fmt.Println(matrixPrint(matrix))
}
//Rotate clockwise to print multidimensional array
func matrixPrint(matrix [][]int)[]int{
	if len(matrix) == 0 || len(matrix[0]) == 0{
		return []int{}
	}
	row, col := len(matrix),len(matrix[0])
	var res []int
	var rowStart,rowEnd,colStart,colEnd = 0,row-1,0,col-1
	var times = row * col
	for index, i,j:=0,rowStart-1,colStart-1;index < times;{
		for i,j=i+1,j+1;index <times &&  j <= colEnd;j++{
			res = append(res,matrix[i][j])
			index++
		}
		rowStart++
		for i,j=i+1,j-1;index<times && i <= rowEnd;i++{
			res = append(res,matrix[i][j])
			index++
		}
		colEnd--
		for i,j=i-1,j-1;index <times && j >= colStart;j--{
			res = append(res,matrix[i][j])
			index++
		}
		rowEnd--
		for i,j=i-1,j+1;index <times && i >= rowStart;i--{
			res = append(res,matrix[i][j])
			index++
		}
		colStart++
	}
	return res
}

Know one side:

About the lock: the case of non release; It is not a case of self release;

mysql: what is the implementation of transactions and four features? Four isolation States and how MVCC can realize repeatable reading and read submission; What is stored in the B + tree in the specific query process of the joint index?

Redis: cache invalidation; How to deal with avalanche and breakdown?

RocketMQ: how to ensure that messages are not lost? How do consumers spend?

Make a question: merge multiple ordered sub arrays (the interviewer who knows Hu is so cool. He said that the interview comes here first and leave an after-school question for me to do on the cloud document, so I can check the data)

/*
There are several ordered subarrays, which are combined into an ordered array, M subarray, and the average length of the subarray is N
 For example:
A: 1,  3,  5, 7, 9
B: 2,  3, 5, 6,  8
C: 4, 6, 8, 9
D: ...
...
Goals: 1, 2, 3, 3, 4, 5, 5, 6, 6, 7, 8, 8, 9, 9
 Requirements: time complexity and space complexity are the best.

 */

package main

import "fmt"

func main(){
	TestMergeArrays()
}

func TestMergeArrays(){
	var arrays = [][]int{{1,  3,  5, 7, 9},{2,  3, 5, 6,  8},{4, 6, 8, 9}}
	res := MergeArraysByQuickSort(arrays)  // Idea 1: add all elements of the subarray to an array and use fast sorting for the array
	fmt.Println(res)

	res = MergeArraysByMergeSort(arrays) // Idea 2: merge adjacent subarrays and gradually merge them into large ordered subarrays
	fmt.Println(res)
}

func MergeArraysByQuickSort(arrays [][]int)[]int{
	if len(arrays) == 0{
		return []int{}
	}
	var mergeArrRes []int
	for _, arr := range arrays{
		mergeArrRes = append(mergeArrRes,arr...)
	}
	QuickSort(&mergeArrRes)
	return mergeArrRes
}

func QuickSort(arr *[]int){
	if len(*arr) == 0{
		return
	}
	quick(arr,0,len(*arr)-1)
}
//In the method of digging and filling, select a base number (usually the first one), and the two pointers point to the head and tail,
//Those smaller than the cardinal number are placed on the left of the cardinal number, and those larger than the cardinal number are placed on the right of the cardinal number,
func quick(arr *[]int,left,right int){
	low,high := left,right
	pivot := (*arr)[low]
	for low < high{
		for low<high &&  (*arr)[high] > pivot{
			high --
		}
		if low < high{
			(*arr)[low] = (*arr)[high]
			low++
		}
		for low<high &&  (*arr)[low] < pivot{
			low ++
		}
		if low < high{
			(*arr)[high] = (*arr)[low]
			high --
		}
	}
	(*arr)[low] = pivot
	if low - left > 1{
		quick(arr,left,low-1)
	}
	if right-low > 1{
		quick(arr,low+1,right)
	}
}


func MergeArraysByMergeSort(arrays [][]int)[]int{
	if len(arrays) == 0{
		return []int{}
	}
	var res []int
	for _,array := range arrays{
		res = merge(res,array)
	}
	return res
}

func merge(arr1,arr2 []int)[]int{
	var brr []int
	var (
		i = 0
		j = 0
		len1 = len(arr1)
		len2 = len(arr2)
	)
	for i<len1 && j <len2{
		if arr1[i] < arr2[j]{
			brr = append(brr,arr1[i])
			i++
		}else{
			brr = append(brr,arr2[j])
			j++
		}
	}
	if i<len1{
		brr = append(brr,arr1[i:]...)
	}
	if j<len2{
		brr = append(brr,arr2[j:]...)
	}
	return brr
}

Know two sides

Starting from the project, why use the set set of redis, the underlying data structure of set, the time efficiency of insertion and search, the command to set the expiration time, and the strategy of regular deletion; Why use beanstalk? Have you ever encountered message loss? How to guarantee? What if it's lost? How does mysql ensure that messages are not lost? redolog, undolog, where is it written? Write an SQL query that contains a, B, C in a field?

Know more than one side (vote again!)

Algorithm: do a binary search problem, using circular and recursive methods

package main

import (
	"fmt"
)
/*
Given an n-element ordered (ascending) integer array nums and a target value target, write a function to search the target in nums. If the target value exists, return the subscript, otherwise return - 1.

Example 1:
Input: num = [- 1,0,3,5,9,12], target = 9
 Output: 4
 Explanation: 9 appears in nums with subscript 4

Example 2:
Input: num = [- 1,0,3,5,9,12], target = 2
 Output: - 1
 Explanation: 2 does not exist in nums, so - 1 is returned

Tips:
You can assume that all elements in nums are not repeated.
n Will be between [1, 10000].
nums Each element of will be between [- 9999, 9999].
 */
func FindTargetByArray(nums []int, target int)int{
	if len(nums) == 0 || target < nums[0] || target > nums[len(nums)-1]{
		return - 1
	}
	var start, mid, end = 0, 0, len(nums)-1
	for start <= end {
		mid = (start + end) / 2
		if nums[mid] == target{
			return mid
		}
		if nums[mid] > target{
			end = mid - 1
		}
		if nums[mid] < target{
			start = mid + 1
		}
	}
	return -1
}

func FindTarget(nums []int, target int)int{
	if len(nums) == 0 || target < nums[0] || target > nums[len(nums)-1]{
		return - 1
	}
	var FindTargetByDigui func(int, int)int
	FindTargetByDigui = func(start int,end int)int{
		var mid int
		var res = -1
		if start <= end{
			mid = (start + end) / 2
			if nums[mid] == target{
				return mid
			}
			if nums[mid] > target{
				res = FindTargetByDigui(start,mid-1)
			}
			if res == -1 && nums[mid] < target{
				res = FindTargetByDigui(mid+1,end)
			}
		}
		return res
	}
	res := FindTargetByDigui(0,len(nums)-1)
	return res
}

func TestFindTarget(){
	var nums = []int{-1,0,3,5,9,12}
	target := 9
	fmt.Println(FindTarget(nums,target))

	target = 2
	fmt.Println(FindTargetByArray(nums,target))


}

func main(){
	TestFindTarget()
}

go: if slice is a value copy or a reference copy as a formal parameter, (the slice object will be copied, but the array pointer inside the slice structure still points to the original address, without copying the array itself) the bottom layer and capacity expansion of the map, sync The difference between map and map, what optimization has been done, the bottom layer of channel, the specific reading and writing process, and the difference between net package and gin

The difference between redis persistence and RMQ persistence and Kafka persistence

mysql deep page, this sql execution process

select * from table_a where a = 1 and b = 2 limit 100000, 10

Open-minded Technology:

Go: the specific object (heap) to be recycled by GC; What is the difference between heap and stack? Memory escape? Say slice, bottom? Say something; How much memory does struct {} occupy? (0) how to use it? (when the placeholder is used, the actual data is not stored in the pipeline, but only used as a signal) has pprof been used? What library do you usually use? What is the problem with the following code and how to modify it?

func main() {
	var slice = []int{100, 200, 300, 400, 500}
	wg := sync.WaitGroup{}
	for i, s := range slice {
		go func() {
	        wg.Add(1)
			defer wg.Done()
			fmt.Println("[", i, "]", ":", s)
		}()
	}
	wg.Wait()
}
//Nothing will be printed. The main process ends directly. Modify:

func main() {
	var slice = []int{100, 200, 300, 400, 500}
	wg := sync.WaitGroup{}
	wg.Add(len(slice))
	for i, s := range slice {
		go func(i,s int) {
			defer wg.Done()
			fmt.Println("[", i, "]", ":", s)
		}(i,s)
	}
	wg.Wait()
}

Explain how rocket MQ works? How to solve the problem of full redis memory? Redis persistence?

mysql: let's talk about the index and B + tree. When will the index be built? How to tune? Have you ever used expire?

Scenario Title: there are 100000 goruntines now. How to ensure that at most 100 goruntines are running at the same time? (use a channel with a length of 100 to write data inside Ctrip. Read the data before the collaboration exits. Because the reading and writing channel is atomic, it can ensure that only 100 collaboration processes are running and the rest will be blocked)

If the server running on Linux hangs, how do you usually check it? Command to view log? (tail -f real-time view, cat |grep filter view)

Please use object-oriented programming to find the two numbers in the array and = target

package main

import (
	"errors"
	"fmt"
	"sync"
)

func main() {
    Test()
}

type ArrayInter interface {
	FindTwoNums()(int,int,error)
}

type ArraySum struct {
	Array []int
	Target int
}

func(as *ArraySum)FindTwoNums()(int,int,error){
	if len(as.Array) == 0{
		return -1,-1,errors.New("No value in array!")
	}
	var myMap = make(map[int]int)  // Difference: index
	for index,num := range as.Array{
		if anaIndex,exist:=myMap[num];exist{
			return anaIndex,index,nil
		}
		myMap[as.Target-num] = index
	}
	return -1,-1,errors.New("There is no number that meets the requirements!")
}

func Test(){
	var myStruct = ArraySum{[]int{1,3,5,2,4,6},11}
	var myInter ArrayInter = &myStruct
	index1,index2,err := myInter.FindTwoNums()
	if err != nil{
		fmt.Println(index1,index2,err.Error())
	}else{
		fmt.Println(index1,index2,nil)
	}


	var myStruct2 = ArraySum{[]int{1,3,7,2,5,15},11}
	var myInter2 ArrayInter = &myStruct2
	index1,index2,err = myInter2.FindTwoNums()
	if err != nil{
		fmt.Println(index1,index2,err.Error())
	}else{
		fmt.Println(index1,index2,nil)
	}


	var myStruct3 ArraySum = ArraySum{[]int{},11}
	var myInter3 ArrayInter = &myStruct3
	index1,index2,err = myInter3.FindTwoNums()
	if err != nil{
		fmt.Println(index1,index2,err.Error())
	}else{
		fmt.Println(index1,index2,nil)
	}

}

Open vision on two & three sides:

How to call the micro service in five minutes and return to the design scenario?

What kind of data structure is needed in the project?

What is the difference between TCP long connection and short link?

What do you think the design of gorm is unreasonable? (0 value problem)

shopee side:

Talk about the project: talk about the time-consuming process of mysql query in detail, and why redis is fast?

Did two questions: find the nearest public parent node of two nodes; Transforming binary search tree into binary accumulation tree;

(I don't understand this company. The two algorithms have been written. Why is there no news after one side

Horizon side:

Project: the master worker mode is used. Have you ever encountered the situation that there are too many data and consumers have no time to consume? (the manager increases or decreases the number of workers according to a certain load balancing strategy) why use beanstalk as a distributed message queue? How to use redis to complete distributed message queuing? (I said list), what if you want to find 100 data out of 10000 data? (Zset) the underlying data structure of Zset (hash + jump table)

sso mechanism( What is single sign on (SSO) , what is the difference between jwt and cookie? http request method, response code,

Go: how to prevent the expansion of slice, the bottom layer and memory leakage (without waiting for the completion of the collaborative process)? (wait group) how to detect the difference between (pprof) processes and threads, GMP model

Process: process is the basic unit of resource allocation in the system, with independent memory space.

Threads: threads are CPU The basic unit of scheduling and dispatch. Threads are attached to processes, and each thread will share the resources of the parent process.

Collaborative process: a collaborative process is a lightweight thread in user mode. The scheduling of collaborative processes is completely controlled by the user. Switching between collaborative processes only needs to save the task context without kernel overhead.

docker: build,push,run,load,save, how to reduce the size of the image? (use the & symbol to connect multiple RUN commands together?)

mysql: what is the InnoDB lock? There are high concurrent read and write requests for the inventory field of the commodity table. What should I do? (split the inventory into multiple rows to reduce the concurrency of one row lock)

Fast dictation;

Please use factory mode to design a method that can return different types?

package main

import (
	"encoding/json"
	"encoding/xml"
)

type ReturnSt interface {
	output ()interface{}
}

type JsonOutPut struct {
	Data string `json:"data"`
}

type XmlOutPut struct {
	Data string
}

func (jt *JsonOutPut)output()interface{}{
	res, err := json.Marshal(jt)
	if err != nil{
		return nil
	}
	return res
}

func (xt *XmlOutPut)output()interface{}{
	res, err := xml.Marshal(xt)
	if err != nil{
		return nil
	}
	return res
}

func ReturnApi(DataType string)interface{}{
	var res interface{}
	var myInterface ReturnSt
	switch DataType {
	case "json":{
		var jt = JsonOutPut{"hello world"}
		myInterface = &jt
	}
	case "xml":{
		var xt = XmlOutPut{"hello world"}
		myInterface = &xt
	}
	}
	myInterface.output()
	return res
}

Two sides of the horizon:

He said a series of scenarios and asked me how to locate the request from the log. He wanted me to answer the link monitoring

When redis memory is full, I answer LRU and horizontal expansion

How does the cluster ensure high availability? I answered the AOF RDB sentry

Can Mysql create a table without setting a primary key? What is the purpose of the primary key? What happens to deadlock?

Write code: read a large file. Each line in the file is the user ID. a known RPC interface obtains Mail according to the ID and outputs the Mail file corresponding to the ID file

package main

import (
	"bufio"
	"errors"
	"os"
	"sync"
)

func GetMailById(id string)string{
	return ""
}

func readDoc(p int64, wg *sync.WaitGroup, myChan *chan string)error{
	defer wg.Done()
	file, err := os.Open("./userid")
	file.Seek(p,1)
	//mailfile ,err := os.Open("./mail"+string(p)+".txt")
	if err != nil{
		return errors.New("open file error")
	}
	defer file.Close()
	buf := bufio.NewReader(file)
	for{
		id ,_, err := buf.ReadLine()  ///
		if err != nil{
			return errors.New("open file error")
		}
		//mail := GetMailById(string(id))
		//mailfile.Write([]byte(mail))
		*myChan <- string(id)
	}
}


func GetMail(){
	var wg sync.WaitGroup
	wg.Add(10)
	var myChan  = make(chan string)
	for i:=0;i<100000;i+=10000{
		go readDoc(int64(i),&wg,&myChan)
	}
	wg.Wait()

}

Baidu side:

Go: let's talk about map. Is it concurrency safe? How to achieve concurrency security? (I answer using sync.map or lock control) what locks are commonly used? How does the read-write lock work? One case: how to deal with the situation that a large number of coroutines add read locks to an object, resulting in the failure of write locks? What is the difference between CO process threads? What are the costs of creating? What are the features of the defer keyword? In one case, a function is created with a coroutine, and Panic is triggered in the coroutine. Can the outer function capture it? (I can't catch it!) Are map and slice value passing? Slice capacity expansion mechanism? What happens when the go run command is executed?

package main

import "fmt"

func expe (){
	var myChan chan int
	close(myChan)
	myChan <- 1
}

func main(){
	defer fmt.Print("Before the main function exits")
	defer func() {

		if r := recover(); r != nil {
			fmt.Printf("Errors captured:%s\n", r)
		}
	}()
	go expe()
}

Redis: let's talk about the data structure. What's the bottom layer of zset? Jump table insertion, find time complexity? zset insertion, search time complexity? Redis avalanche, breakdown, penetration and prevention methods? (penetration: check the parameters in the interface) how to check the illegal parameters with large ID? (a counter is used to store the maximum value of ID, and the counter is stored in redis)

Redis command time complexity query table - liuxinyu123 - blog Park

Talk about Mysql index and redis jump table -- the ordered set zset data structure of redis adopts the jump table principle at the bottom, and the time complexity is O (logn) (ALI) - aspirin - blog Garden

https, what is the specific certificate verification process? Can you talk about CSRF attack?

Linux: has the awk command been used? How to find the PV of an IP in the log?

Linux server troubleshooting ideas and common commands - xiaodai - blog Garden

Code implementation: a producer obtains a random number from [- 50, 50] each time, and two consumers read it concurrently. One only consumes positive numbers and the other only consumes negative numbers. (this inscription is thin and broken)

package main

import (
	"fmt"
	"math/rand"
	"sync"
	"time"
)

func CreateRandom()int{
	res := rand.Int31n(50)
	zf := rand.Int31n(2)
	switch zf {
	case 0: res = 0 - res
	}
	return int(res)
}

type Producer struct{}

type Consumer struct {

}

func (p *Producer)Produce(zhengChan *chan int,fuChan *chan int){
	data := CreateRandom()
	if data >= 0 {
		*zhengChan <- data
	}else{
		*fuChan <- data
	}
}

func (c *Consumer)ConsumeZheng(zhengChan * chan int,wg *sync.WaitGroup){
	for data := range *zhengChan{
		fmt.Println("consumer zheng:", data)
		wg.Done()
	}
}

func (c *Consumer)ConsumeFu(fuChan * chan int,wg *sync.WaitGroup){
	for data := range *fuChan{
		fmt.Println("consumer fu:", data)
		wg.Done()
	}
}

func main(){
	var p Producer
	var zhengChan = make(chan int,100)
	var fuChan = make(chan int,100)
	var wg sync.WaitGroup
	wg.Add(100)
	var c Consumer
	go c.ConsumeZheng(&zhengChan,&wg)
	go c.ConsumeFu(&fuChan,&wg)
	rand.Seed(time.Now().Unix())
	for i:=0;i<100;i++{
		//time.Sleep(time.Second*1)
		p.Produce(&zhengChan,&fuChan)
	}
	wg.Wait()
	close(zhengChan)
	close(fuChan)

}

Keywords: Go microsoft linq

Added by damic on Thu, 03 Mar 2022 08:56:35 +0200