Linux - Symbolic Link

ln -s <path to the file/folder to be linked> <the path of the link to be created>

例如

ln -s hello.txt newhello.txt     

可以用 ls -l 列出檔案查看: 最前面的 l 代表這個檔案是 symlink,後面的箭頭代表這個檔案指向哪個檔案。

unlink <path-to-symlink>

直接使用 rm 指令也行。

find . -type l ! -exec test -e {} \; -print

find -L

- L flag 代表搜索過程中會遵循 symlink,也就是進入 symlink 指向的資料夾/文件作進一步搜索,使用這個 flag 可能會進入無限迴圈,或是不經意地破壞作業系統,應避免使用!

Reference

How can I find broken symlinks Symlink Tutorial in Linux – How to Create and Remove a Symbolic Link

Linux - Shell input & output

把指令的輸出存到某個檔案:

command > file

例如:

echo hello > hello.txt

可以用 cat hello.txt 印出檔案內容。

注意!hello.txt 如果已經存在,會覆蓋掉原本的檔案!

不想覆蓋掉檔案,而是加在原檔案後面的話,將原本的指令的 > 改為 »

例如:

echo hello >> hello.txt

Pipe character

將某指令的輸出當作另一個指定的標準輸入,可以使用 pipe character (|)

例如:

cat hello.txt | wc

wc 會印出會印出他的行數、單詞數、字符串數(包含 newline),會發現是 1, 1, 6 把字串送進檔案、不包含 newline 的另一個作法是這個指令:

printf hello > hello.txt    

Reference

[Linux]cat 搭配wc用法

Linux - Manual & Info

查看 ls 指令的 manual page :

man ls 

最上方 LS 旁邊的 (1) 代表這個指令他的類別,1 是 User commands (Programs)。詳細分類如下:

不確定指令名字的時候,使用關鍵字搜尋 manual page:

man -k keyword

另一種可以使用的指令是 info :

info ls

Reference

man-pages(7) — Linux manual page

React - 使用 Wagmi 連接錢包並保持連線

依照 wagmi document 指示安裝好 package後,首先在 React 建立連線錢包按鈕的 Component:

import { useAccount, useConnect } from 'wagmi'
import { InjectedConnector } from 'wagmi/connectors/injected'
import React from 'react'
import { useDisconnect } from 'wagmi'

export default function ConnectButton() {
  const { address, isConnected } = useAccount()
  const { connect } = useConnect({
    connector: new InjectedConnector(),
  })

  const { disconnect } = useDisconnect()

  async function connectWallet() {
    try {
      connect()
      localStorage.setItem('isWalletConnected', true) 
      // 連接錢包後將 localStorage 的 isWalletConnected 欄位設定為 true
    } catch (ex) {
      console.log(ex)
    }
  }

  async function disconnectWallet() {
    try {
      disconnect()
      localStorage.setItem('isWalletConnected', false)
      // 解除連接錢包後將 localStorage 的 isWalletConnected 欄位設定為 false
    } catch (ex) {
      console.log(ex)
    }
  }

  if (isConnected) return( // 如果 isConnected == true 表示在連線狀態,再次點擊會斷開連接
    <div>
      <button  id='wallet_address' onClick={() => disconnectWallet()}>Connected to {address}</button>
    </div>
  )
  return(
    <div>
      <button id='personal_btn' onClick={() => connectWallet()}>Connect Wallet</button>
    </div>
  )
}

在需要連接錢包、保持連線的地方新增以下程式碼

import * as React from 'react';
import { useEffect } from "react";
import { InjectedConnector } from 'wagmi/connectors/injected'
import ConnectButton from "./component/connect";
import { useConnect  } from 'wagmi'

function Main() {
	const { connect } = useConnect({
    connector: new InjectedConnector(),
  })

	useEffect(() => { 
    // 每次進入頁面都檢查 localStorage 是否有已連線的資訊,沒有則自動連線錢包
		const connectWalletOnPageLoad = async () => {
			if (localStorage?.getItem('isWalletConnected') === 'true') {
				try {
					connect()
				} catch (ex) {
					console.log(ex)
				}
			}
		}

		connectWalletOnPageLoad()
	}, [])

	return(
		<div>
			<div><ConnectButton /></div> 
		</div>
	  );
};

export default Main;

Reference

Keep Wallet Connected on Page Refresh — Web3 Dev

Go - HTTP POST/GET request 筆記

我的 Go 版本:go version go1.20.1 darwin/amd64

GET

package main

import (
    "fmt"
    "io/ioutil"
    "log"
    "net/http"
)

func main() {

    resp, err := http.Get("https://weippig.com/")

    if err != nil {
        log.Fatal(err)
    }

    defer resp.Body.Close()

    body, err := ioutil.ReadAll(resp.Body)

    if err != nil {
        log.Fatal(err)
    }

    fmt.Println(string(body))
}

POST

假設要發送 POST 請求給某個叫做 http://apple.com 的 url,並且使用 JSON 格式傳送字串 merchant_id 和 merchant_trade_no 以及數字 total_amount:

import (
    "bytes"
    "encoding/json"
    "fmt"
    "log"
    "net/http"
    "io/ioutil"
)

func main() {
    url := "http://apple.com"
    values := map[string]interface{}{
		"merchant_id":       "111",
		"merchant_trade_no": "123",
		"total_amount":      1,
	 }

    json_data, err := json.Marshal(values)

    if err != nil {
        log.Fatal(err)
    }

    resp, err := http.Post(url, "application/json",
        bytes.NewBuffer(json_data))

    if err != nil {
        log.Fatal(err)
    }

    body, error := ioutil.ReadAll(resp.Body)
    if error != nil {
      fmt.Println(error)
    }

    resp.Body.Close()

    fmt.Println(string(body))
}