Docker Image & Container

 Docker Image: single file with all the dependencies and config required to run a program.

Docker container: Instance of an image. Runs a program.













kernel是作業系統的核心元件,負責處理應用程式和硬體之間的溝通。











只要運行docker, 一個linux VM也在你的電腦上運行。

在linux vm內,會有一個linux kernel負責在容器中運行和托管進程。


什麼是Goroutine?


什麼是Goroutine?

 











以這張圖為例,最上面的Go program就是我們寫的程式。

在 Go 語言中,每個 Go program都運行在一個稱為 Go runtime 的運行系統之上。

Go runtime是什麼?

In Go, the runtime is an essential part of the language that manages memory allocation, garbage collection, and scheduling of goroutines. The Go program interacts with the runtime through the Go standard library, which provides an abstraction layer over the runtime. The Go program is compiled into machine code and executed by the operating system. When the program starts, it initializes the Go runtime, which sets up the memory management system, creates the main goroutine, and starts the scheduler.

The Go program can create additional goroutines to run concurrently with the main goroutine. The runtime manages the scheduling of these goroutines, ensuring that they execute efficiently and safely. The runtime also handles the communication and synchronization between goroutines through channels and other synchronization primitives. Therefore, the Go program and the Go runtime are tightly integrated, with the runtime providing the underlying infrastructure that enables the program to execute efficiently and concurrently.

然後最底層是Operating system, 程式碼會被編譯成machine code在OS執行。

在Scheduler和OS之間就有一個稱為Thread的構造。無論使用哪種OS,Thread都是OS用來管理其併發性的工具。當OS跟蹤同時發生的多個事情時,它們使用Thread來區分這些工作。












Scheduler的工作是將這些 goroutine(也稱為虛擬線程,這些線程純粹由我們的 Go program維護)映射到OS的thread上。然而,goroutine 有限制,因為它不直接與OS互動,所以它本身無法做任何事情。這就是Scheduler發揮作用的地方。Scheduler會將 goroutine 映射到OS的thread上。


 








goroutine跟thread差異

通常thread有固定的stack space, 約1mb

而goroutine的stack space是變動的, 起始大小為2kb

thread被os管理,goroutine被 Go runtime管理

thread比較消耗資源, goroutine相較起來較少



ps. go runtime簡介













Js object array 多條件篩選且條件可能為空

 最近在工作上遇到需要多條件篩選,且條件為空就不進行判斷,花了一點時間才研究出怎麼做

以下是解法

const originItems =
[{id:1, live:true, name:"test1",color:"red",size:"large"},
{id:2, live:true,name:"test2",color:"blue",size:"large"},
{id:3, live:true,name:"test3",color:"green",size:"medium"},
{id:4, live:false, name:"test4",color:"red",size: "small"},
{id:5, live:false, name:"test5",color:"blue",size: "small"},
{id:6, live:false, name:"test6",color:"green",size:"medium"}];

const sort = {live: true, color: "", size:"large"}

const result = originItems.filter( (o) =>
(!sort.live || o.live === sort.live) && (!sort.color || o.color === sort.color)
&& (!sort.size || o.size === sort.size)
)

console.log(result)

結果如下:

  1. length: 2

useEffect的三種dependency差異


第一種方式因為傳入的dependency是empty array

所以只會執行一次 

useEffect(() =>{

console.log("Hi)

},[])



第二種方式當每一次dependency變動時就會執行一次useEffect
useEffect(() =>{

console.log("Hi)

},[dependency])


第三種方式會變成無限迴圈...慎用
useEffect(() =>{

console.log("Hi)

},)

Js轉換時間格式為YYYY-MM-DD HH:MM:SS

身為前後端都要寫的工程師,時間處理總是會碰到的

以下是簡單的方法將Js 的Date 轉為 YYYY-MM-DD HH:MM:SS 格式的寫法

const d = new Date()
const formatedTimestamp = (d)=> {
const date = d.toISOString().split('T')[0];
const time = d.toTimeString().split(' ')[0];
return `${date} ${time}`
}

Css Combinators

Css Combinators 有以下四種


  • descendant selector (space)
  • child selector (>)
  • adjacent sibling selector (+)
  • general sibling selector (~)

以這個html為例,html都是巢狀,所以是一層包一層的結果,如果我們想要選擇下層的標籤內容,可以使用descendant selector  語法是:在上層跟下層中間加上一個”空白”


<!DOCTYPE html>
<html>
<head>
<style>
div p {
background-color: yellow;
}
</style>
</head>
<body>

<h2>Descendant Selector</h2>

<p>The descendant selector matches all elements that are descendants of a specified element.</p>

<div>
<p>Paragraph 1 in the div.</p>
<p>Paragraph 2 in the div.</p>
<section><p>Paragraph 3 in the div.</p></section>
</div>

<p>Paragraph 4. Not in a div.</p>
<p>Paragraph 5. Not in a div.</p>

</body>
</html>

output 





將style更換成child selector如下
<style>
div > p {
background-color: yellow;
}
</style>

可以發現畫面paragraph3沒有變成黃色


因為
paragraph3外面多了一層<section> 所以沒辦法直接被child selector 選擇



再將style更改如下
div + p {
background-color: yellow;
}


我們這邊用adjacent sibling selector,這會選擇指定相鄰的元素,在<div>後面的<p>被選擇了
</div>

<p>Paragraph 4. Not in a div.</p>
所以只有paragraph4被選中


最後是general sibling selector,只要相同層級並且順序在自己後面,就可以使用
<div>
<p>Paragraph 1 in the div.</p>
<p>Paragraph 2 in the div.</p>
<section><p>Paragraph 3 in the div.</p></section>
</div>

<p>Paragraph 4. Not in a div.</p>
<p>Paragraph 5. Not in a div.</p>
如圖,paragraph4跟paragraph5都跟div同個層級,又在div後面,所以會被選中




Css Selector

 範例如下

index.html

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>uHost</title>
<link rel="shortcut icon" href="favicon.png">
<link rel="stylesheet" href="index.css">
</head>

<body>
<main>
<section>
<h1>Get the freedom you deserve.</h1>
</section>

<section class="test_class">
<h1 class="test_class_h1">Use class</h1>
</section>

<section id="test_id">
<h1 id="test_id_h1">Use id</h1>
</section>

<button disabled>Use attribute</button>
</main>
</body>

</html>


index.css

section{
background-color: bisque;
}
h1{
color: aqua;
}

.test_class{
background-color: cornsilk;
}

.test_class_h1{
color: aquamarine;
}

#test_id{
background-color: black;
}

#test_id_h1{
color: aliceblue;
}

button[disabled]{
color: aquamarine;
}


section 代表選擇所有 section element
.test_class 代表選擇所有class為test_class的element
#test_id 代表選擇所有id為test_id的element
button[disabled] 代表選擇所有 attribute有disabled的 button