An extract of the information flow dated March 20, 2021

Released the original address: Digest | Golang interview questions to crack, how clever to take notes

Learn Golang every day

How to Solve the 25 Golang Interview Questions

原文 : How To Crack the Top 25 Golang Interview Questions

  • What are the benefits of using Go compared to other languages?

    • Unlike other languages that started out as academic experiments, the Go code was designed to be practical. Every functional and syntactic decision is designed to make the programmer’s life easier. Golang is optimized for Concurrency, and it works well at scale. Automatic garbage collection is more efficient than Java or Python because it is executed at the same time as the program.
  • The data type of Go

    • Method, Boolean, Numeric, String, Array, Slice, Struct, Pointer, Function, Interface, Map, Channel
  • How do YOU implement inheritance with Go

    • type Animal struct {
          / /...
      }
      func (a *Animal) Eat(a){... }func (a *Animal) Sleep(a){... }func (a *Animal) Run(a){... }type Dog struct {
          Animal
          / /...
      }
      Copy the code
  • What is an Interface and how does it work

  • How do I check variable types at run time?

    • package main
      import "fmt"
      func do(i interface{}) {
          switch v := i.(type) {
          case int:
              fmt.Printf("Double %v is %v\n", v, v*2)
          case string:
              fmt.Printf("%q is %v bytes long\n", v, len(v))
          default:
              fmt.Printf("I don't know  type %T!\n", v)
          }
      }
      func main(a) {
          do(21)
          do("hello")
          do(true)}Copy the code
  • What is the loop structure in Go?

    • Go has only one loop structure: the for loop. for i := 0; i < 10; i++ {}
  • Permutations prints all permutations of slice characters or strings.

    • // Perm calls f with each permutation of a.
      func Perm(a []rune, f func([]rune)) {
              perm(a, f, 0)}// Permute the values at index i to len(a)-1.
      func perm(a []rune, f func([]rune).i int) {
              if i > len(a) {
                      f(a)
                      return
              }
              perm(a, f, i+1)
              for j := i + 1; j < len(a); j++ {
                      a[i], a[j] = a[j], a[i]
                      perm(a, f, i+1)
                      a[i], a[j] = a[j], a[i]
              }
      }
      func main(a) {
      Perm([]rune("abc"), func(a []rune) {
              fmt.Println(string(a))
      })
      }
      Copy the code
  • Swap the values of two variables without a temporary variable

    • func main(a) {
         fmt.Println(swap())
      }
      func swap(a) []int {
            a, b := 15.10
         b, a = a, b
         return []int{a, b}
      }
      Copy the code
  • Reverse the order of slice

    • func reverse(sw []int) {
              for a, b := 0.len(sw)- 1; a < b; a, b = a+1, b- 1 {
                      sw[a], sw[b] = sw[b], sw[a]
              } 
      }
      func main(a) { 
          x := []int{3.2.1} 
          reverse(x)
          fmt.Println(x)
      }
      Copy the code
  • Explain the difference between concurrent and parallelism in Golang.

    • Concurrency means that your program can handle multiple tasks at the same time, and parallelism means that your program can use multiple processors to perform multiple tasks at the same time. So parallelism can be one way to implement concurrency properties, but it’s only one of many things you can do. The key tools for concurrency in Golang are Goroutines and channels. Goroutines are concurrent lightweight threads, and channels allow Goroutines to communicate with each other during execution.
  • Implement concurrent merge sort schemes using Goroutines and channels.

    • Code refer to appendix
  • Implement the SumOfSquares function, which takes an integer C and returns the sum of all squares between 1 and C, using select statements, goroutines, and channels. For example, typing 5 returns 55 because 1^2 + 2^2 + 3^2 + 4^2 + 5^2 = 55$.

    • Code refer to appendix

Learn soft skills every day

How to Take Notes Intelligently — 10 principles to revolutionize Your note-taking and writing.

How To Take Smart Notes: 10 Principles To Revolutionize Your note-taking and Writing

  • Principle 1: Writing is not a result of thinking, but a Medium for thinking.

    • If you want to learn and remember something in the long term, you have to write it down. If you want to understand an idea, you have to translate it into your own language.

    • If you want to learn and remember something long-term, you have to write it down. If you want to understand an idea, you have to translate it into your own words.

  • Principle 2: Write as the Only thing matters.

    • The purpose of research is to produce public knowledge that can be examined and tested. To do that, you have to write it down. Once written down, it doesn’t matter what the author means — only the actual words on the paper matter.

      In academia and science, almost all research is done with the goal of eventual publication, Ahrens notes. “There is no such thing as private knowledge in academia. An unknown idea is as good as an idea you never had.”

  • Rule 3: No one can start with a blank

    • You have to immerse yourself in research before you know how to write a good question. And the decision to read one topic from another doesn’t just appear out of thin air. It usually comes from an existing interest or understanding. In fact, every intellectual work begins with a prior idea.

      This is the tension at the heart of the creative process. Before you choose what you will write, you must do your research. Ideally, you should start your research well in advance, so that once you decide on a topic, you have weeks, months or even years of rich material to draw on. That’s why an external system to record your research is so critical.

  • Principle 4: Our tools and techniques are only valuable in the workflow

    • Just because writing is not a linear process doesn’t mean we should do it haphazardly. We need a workflow — a repeatable process of gathering, organizing, and sharing ideas.

      Writing is often taught as a series of “tricks and tricks “– brainstorming, outlining, using three-paragraph structures, repeating bullet points, using vivid examples, and setting a timer. Each individual technique may make sense, but without considering how they fit together as a whole, they add more work.

  • Principle 5: Standardization contributes to creativity

    • This means that when it comes time to write, they first undertake a mammoth project of collecting and organizing these scattered notes. Notes are like containers for ideas. Instead of inventing a new way to take notes for every material you read, use a completely standardized and predictable format every time. No matter what the notes contain, what topic they relate to, or what medium they arrive in, you treat each one exactly the same way. By standardizing and simplifying the format of our notes and the steps we take to process them, the real work can begin: thinking, reflecting, writing, discussing, testing, and sharing. This is value-adding work, and now we have time to do it more effectively.
  • Principle 6: Our work gets better when we are exposed to high-quality Feedback

    • The workflow is similar to a chemical reaction. It can be self-sustaining, a kind of virtuous circle in which the positive experience of understanding a text motivates us to move on to the next task, which helps us to do the work we’re doing better, which in turn makes us more likely to enjoy our work, and so on.
  • Principle 7: Work on multiple projects simultaneously

    • We encounter an endless stream of new ideas, but only a tiny fraction of them are useful and relevant to us at any given moment. Since the only way to know what insights a book contains is to read it, you might as well read and take notes efficiently. Taking the extra time to write down the best ideas you come across — whether or not you know how they will eventually be used — will greatly increase your chances of “bumping into” them in the future.
  • Rule 8: Organize your notes by context, not by topic

    • The classic mistake is to organize them into more and more specific themes and sub-themes. This makes it seem less complicated, but it can quickly become overwhelming. The more notes you pile up, the smaller and narrower your subtopics become, limiting your ability to see meaningful connections between them. Under this approach, the more notes a person collects, the less accessible and useful they become.
  • Rule 9: Always choose the most interesting path

    • In most cases, Arens points out, students fail not because they are incompetent, but because they lose a personal connection to what they are learning.

      “When high-iq students fail in learning, the most common reasons are that they no longer see the meaning of what they should be learning (see Balduf, 2009) and are unable to connect with their personal goals (Glynn et al., 2009), Or lack the ability to control their own learning and learn on their own terms (Reeve and Jan, 2006; Reeve, 2009).

      Luhmann never forced himself to do anything, only what came easily to his mind.” When I’m stuck in a moment, I leave it and do something else. “” Just like martial arts, if you encounter resistance or opposing forces, you should not push it down, but redirect it to another productive goal.

  • Principle 10: Keep Contradictory thoughts

    • The only criterion for what we preserve is whether it connects with existing ideas and adds to the discussion. When we focus on open connections, uncertain or contradictory data suddenly becomes very valuable. It often raises new questions and opens up new avenues of inquiry.
8 steps to smart Note-taking
  • Temporary notes
  • The literature of notes
  • Permanent note
  • Now add your new permanent notes to the workarea (slip-box)
  • Develop your topics, questions, and research projects from the bottom up in your work area
  • Decide on a topic in the work area and write a topic
  • Turn your notes into rough drafts
  • Edit and proofread your manuscript

Appendix: snippets

  • Charts.css
# Demo: https://jsfiddle.net/wcfez6oq/
  (
      echo "<table class='charts-css bar show-labels show-heading'>"
      echo "<caption>Size of Charts.css releases</caption>"
      for version in $(curl -sS https://github.com/ChartsCSS/charts.css/releases | grep -o releases/tag/[0-9.]* | cut -d/ -f3 | tac); do
        url=https://cdn.jsdelivr.net/npm/charts.css@$version/dist/charts.min.css
        size=$(($(curl -sS "$url" | wc -c)/1024))
        echo "<tr><th>v$version</th><td style='--size: calc($size/ > 100) '${size}KiB</td></tr>"
      done
      echo "</table><link rel=stylesheet href='$url'>"
    ) > size-chart.html
Copy the code
  • Go concurrent sort
package main
import "fmt"
func Merge(left, right [] int) [] int{
  merged := make([] int.0.len(left) + len(right))
  for len(left) > 0 || len(right) > 0{
    if len(left) == 0 {
      return append(merged,right...)
    }else if len(right) == 0 {
      return append(merged,left...)
    }else if left[0] < right[0] {
      merged = append(merged, left[0])
      left = left[1:]}else{
      merged = append(merged, right [0])
      right = right[1:]}}return merged
}
func MergeSort(data [] int) [] int {
  if len(data) <= 1 {
    return data
  }
  done := make(chan bool)
  mid := len(data)/2
  var left [] int
  go func(a){
    left = MergeSort(data[:mid])
    done <- true
  }()
  right := MergeSort(data[mid:])
  <-done
  return Merge(left,right)
}
func main(a){
  data := [] int{9.4.3.6.1.2.10.5.7.8}
  fmt.Printf("%v\n%v\n", data, MergeSort(data))
}
Copy the code
  • Go SumOfSquares implementation
package main
import "fmt"
func SumOfSquares(c, quit chan int) {
  y := 1
  for {
    select {
    case c <- (y*y):
      y++
    case <-quit:
      return}}}func main(a) {
  mychannel := make(chan int)
  quitchannel:= make(chan int)
  sum:= 0
  go func(a) {
    for i := 1; i <= 5; i++ {
      sum += <-mychannel
    }
    fmt.Println(sum)
    quitchannel <- 0
  }()
  SumOfSquares(mychannel, quitchannel)
}
Copy the code

Some of the harvest

  • The Code is the Design

    • There are two parts of craftsman spirit: knowledge and practice. There are tow parts to learning craftemanship: knowledge and work. Your must gain the knowledege of principles, patterns, pactices, and heuristics that a craftsman knows, and you must also grind that knowledge into your fingers, eyes, and gut by working hard and practicing. — Clean Code
  • “Some books are to be tasted, others to be swallowed, and some few to be chewed and digested” – Francis Bacon | The Art of Reading More Effectively and Efficiently