Skip to content

Instantly share code, notes, and snippets.

@wjx0912
wjx0912 / gen_identifying_code.go
Created March 23, 2018 06:04
golang generate identifying code
func makeCode() (code string) {
code = strconv.Itoa(rand.New(rand.NewSource(time.Now().UnixNano())).Intn(899999) + 100000)
return
}
@wjx0912
wjx0912 / golang_timeout1.go
Last active March 23, 2018 07:54
golang timeout1 (每个子任务超时)
package main
import (
"fmt"
"time"
)
func add(ch chan int) {
for i := 0; i < 10; i++ {
ch <- i
@wjx0912
wjx0912 / golang_timeout2.go
Created March 23, 2018 07:54
golang timeout1 (总个子任务超时)
package main
import (
"fmt"
"time"
)
func add(ch chan int) {
for i := 0; i < 10; i++ {
ch <- i
@wjx0912
wjx0912 / golang_closure_error.go
Created March 23, 2018 09:59
golang use closure and wrap error
func listHandler(w http.ResponseWriter, r *http.Request) {
fileInfoArr, err := ioutil.ReadDir("./uploads")
check(err)
locals := make(map[string]interface{})
images := []string{}
for _, fileInfo := range fileInfoArr {
images = append(images, fileInfo.Name)
}
locals["images"] = images
renderHtml(w, "list", locals)
@wjx0912
wjx0912 / c++_boost_split.cpp
Last active March 24, 2018 07:08
c++ boost split demo
std::vector<std::wstring> out_list;
boost::split(out_list, user_wstring1, boost::is_any_of(L" :;()[]<>,-+=/\\\r\n\t"), boost::token_compress_on);
if (2 != out_list.size())
return;
int testvalue = _ttoi(out_list[0].c_str());
@wjx0912
wjx0912 / c++_message_object_management1.cpp
Created March 24, 2018 07:33
c++ message object management 1
// send
PostMessage(MyhWnd, CWM_SOME_ERROR, 0, reinterpret_cast<LPARAM>(new string(the_exception.error_string)) );
// receive
LPARAM CMyDlg::OnMyMessage1(WPARAM, LPARAM lParam)
{
// Put in shared_ptr so it is automatically destroyed.
shared_ptr<string> msg = reinterpret_cast<string*>(lParam);
// Do stuff with message
@wjx0912
wjx0912 / c++_message_object_management2.cpp
Created March 24, 2018 07:34
c++ message object management 2
// send
SendingMethod::SendMsgId( ... )
{
...
std::unique_ptr<MyParams> myParams( new MyParams(value1, value2, value3) );
if (PostThreadMessage(MSG_ID, 0, reinterpret_cast<LPARAM>(myParams.release())) {
myParams.release();    // is postmessage failed
}
@wjx0912
wjx0912 / c++_auto_delete.cpp
Last active March 24, 2018 07:38
c++ auto delete(malloc, new...)
char *buffer = (char *)malloc(len);
std::shared_ptr<void> _free_ptr((void *)buffer, [](void *p){
free(p);
});
// 补充
// FILE *fp = fopen("filepath", "rw");
// use shared_ptr deleter do fclose(fp)
@wjx0912
wjx0912 / c++_tuple_demo.cpp
Created March 24, 2018 07:39
c++ tuple demo
// 创建及获取元组内的对象
std::tuple<double, std::string> tup1(3.14, "pi");
auto tup2 = std::make_tuple("Hello World!", "abc", 3.14, 0);
const char* data = std::get<1>(tup2); // 得到abc
double len = std::get<2>(tup2); // 得到3.14
// 拆箱:tie参数作为左值
auto tup3 = std::make_tuple(3.14, 1, 'a');
double a;
int b;
@wjx0912
wjx0912 / shell_iptables.sh
Created March 24, 2018 07:44
shell script: iptables demo
# 来自65010的请求转发到80
iptables -t nat -A PREROUTING -p tcp --dport 65010 -j REDIRECT --to-port 80
# 来自192.168.8.99的数据全部丢弃
iptables -A INPUT -s "192.168.8.99" -j DROP