2022年12月27日 星期二

2022年12月5日 星期一

golang 的 init()

之前就很好奇,像是 github.com/go-sql-driver/mysql 到底是如何做到只需寫以下幾行,就能夠連線 mysql

import (
	"database/sql"

	_ "github.com/go-sql-driver/mysql"
)

// ...

db, err := sql.Open("mysql", "user:password@tcp(127.0.0.1:3306/my-db)")

2022年11月25日 星期五

Golang slice 研究筆記

以前有研究過 Go silce 的指針,後來看了一些文章,才知道說 Go 本身在底層還有一個 array 來處理 slice。


簡單說,我今天建立一個 slice,實際上同時建立了一個 array

而array 實際上就是個指針


所以執行以下 code 會 print 出同樣的值


arr1 := []int{1, 2, 3}
arr2 := arr1

fmt.Printf("%p\n", &arr1[2])
fmt.Printf("%p\n", &arr2[2])

換言之,arr1[1] = 10 同時會更改到 arr2[1],因為兩者底層的 array 是同一個。

2022年11月2日 星期三

Shell script 在執行程式時,遇到詢問時,如何自動回應

某些程式在執行的過程中,會進行詢問,舉例來說:

$ some-program 
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi mattis velit purus, et bibendum neque dapibus ut. Aliquam erat volutpat. Sed vulputate fermentum eros eu consequat. Vivamus in tellus sit amet dui fringilla tempor sed in lectus. Vestibulum dapibus justo elit, imperdiet vehicula nisi varius id. Nulla posuere ante eu augue commodo ultricies. Nulla nec mauris elit. Donec dolor dolor, interdum eget eleifend ac, rhoncus sit amet libero. Proin ullamcorper luctus libero, id porta tellus ultricies venenatis. Nunc lobortis vulputate diam, in dictum libero scelerisque eget. In et est nulla. Integer venenatis vitae ligula ac faucibus.

----------

Accept? (y/N):

詢問你是否同意,如果同意才能繼續進行。


如果我只是自己開終端機執行也就算了,鍵盤輸入 y 就好,但如果是寫 shell script 就不一樣了


2022年10月28日 星期五

Deno 如何保留 cookie

自己試著用 Deno 寫小機器人

不過不管試幾次,登入後去其他 request 都做不到


我的 code 大致如下

const account = "foo@example.com"
const password = "bar123456789"
const host = "http://localhost:8080"

let cookie = await fetch(`${host}/login`, {
	body: `account=${account}&password=${password}`,
	method: "POST",
	headers: {"Content-type": "application/x-www-form-urlencoded"}
})
	.then(function(resp){
		return resp.headers.get("Set-cookies") || "" 	// 不加上 || "" 的話,cookie 就會被視作 nullable 變數,在 deno 可能會說 warning 或 error
	})

fetch(`${host}/some-request`, {
	headers: {"Cookie": cookie}
})
	.then(function(resp){
		return resp.text()
	}).then(function(data){
		console.log(data)
	})

以上這段 code 最終會 redirect 到首頁而已

因為 cookie 取不到(換言之, cookie 變數一直都會是空字串)

2022年10月18日 星期二

2022年10月17日 星期一

Less import 外部樣式

這是我在使用 Less 時遇到的問題

當我要引用外部的樣式,例如 google font:

@import url('https://fonts.googleapis.com/css2?family=Fredericka+the+Great');

@default-color: #333;

body{
	color: @default-color;
	font-family: 'Fredericka the Great', cursive;
}

會被編譯成如下的 css:

@font-face {
	font-family: 'Fredericka the Great';
	font-style: normal;
	font-weight: 400;
	src: url(https://fonts.gstatic.com/s/frederickathegreat/v15/9Bt33CxNwt7aOctW2xjbCstzwVKsIBVV-9Sk.ttf) format('truetype');
}
body {
	color: #333;
	font-family: 'Fredericka the Great', cursive;
}

可是,外部的 CSS 我並不想要直接將之轉換,我想要匯出的 CSS 依舊使用 @import

2022年10月6日 星期四

2022年10月5日 星期三

Java 用 Stream 將 List<obj> 變成 Map<key, List<obj>>

例如我手邊有 class Student


class Student{
	private int studentID, teacherID;
	private String studentName;
	public Student (int studentID, int teacherID, String studentName){
		this.studentID = studentID;
		this.teacherID = teacherID;
		this.studentName = studentName;
	}
	
	public int getStudentID(){return studentID;}
	public int getTeacherID(){return teacherID;}
	public String getStudentName(){return studentName;}
	
	public String toString(){
		return String.format("[Student %02d: %s]", studentID, studentName);
	}
	
}

假設我手邊有學生清單( List<Student>)

但我想看每個老師底下有哪些學生,換言之,想將之轉為 Map< int (teacherID), List<Student>>

2022年9月26日 星期一

筆記:javascript 中 fetch 與 ajax 的差異

引自以下 twitter

這位 Alex Russell 曾是 Google Chrome 框架的工程師

2022年7月19日 星期二

使用 jQuery Datatables plugin 時,出現 Bean 錯誤

這是最近在處理 spring MVC 專案時遇到的問題。

這個專案前端使用了 jQuery + Datatable plugin;

而在後端,則是這樣寫(FooBarSearch 是一個 Bean)

@RequestMapping(value="/search")
public @ResponseBody ForBarResultBean search(FooBarSearch search) 

2022年6月30日 星期四

GoLang 如何使用自己 localhost 的 module

如果你今天的專案需要引用一個並不在網上,而是自己 localhost 的 module時要如何做?

2022年6月16日 星期四

Go 的 String()

Go 本身可以用 String() 來做到和 Java 等語言的 toString() 一樣的功效。可我最近在使用 String() 發現一個奇特的陷阱

2022年6月15日 星期三

如何讓 tomcat 執行 php

因為某些緣故,需要有個 wordpress 環境,為了方便,所以想說本來電腦就有 tomcat 就看看能不能用 tomcat 直接執行 php。

結果上網查了查,還真的有辦法,在此紀錄一下:

2022年5月24日 星期二

Rust 的“error: linker `link.exe` not found”錯誤

這是照著官網的新手入門,一步一步來時發生的問題。

照著官網建立hello-rust專案後,執行cargo run,就會跳出以下錯誤(我的環境是 win10)

C:\hello-rust> cargo run
   Compiling hello-rust v0.1.0 (C:\hello-rust)
error: linker `link.exe` not found
  |
  = note: program not found

note: the msvc targets depend on the msvc linker but `link.exe` was not found

note: please ensure that VS 2013, VS 2015, VS 2017, VS 2019 or VS 2022 was installed with the Visual C++ option

error: could not compile `hello-rust` due to previous error