Giter Club home page Giter Club logo

blog's Introduction

blog's People

Contributors

lishuhao avatar

blog's Issues

Syncing your fork to the original repository via the browser(待译)

When you create a fork of a repository (here are some instructions) you only have the versions of the files that are in the repository at that time. So if there are any changes in the original repository you may find that your version (your fork) is out of sync.

This can lead to problems when you try to create a pull request.

While GitHub has instructions for merging an upstream repository into your fork they require you to have git installed on your local machine.

Fortunately, when Kirstie reached out to the cool kids from the Mozilla Science Working Open Workshop they gave her the answer. And it turns out there was an answer on the interwebs already: you can update your fork in the browser by following the easy steps laid out in this Stack Overflow answer.

The Stack Overflow link has pictures which are really helpful, but briefly, here's what you do:

  1. Open your fork of the repository.
  2. Click the compare button.
    This will open a page titled Comparing Changes and if you look carefully you'll have jumped to the upstream version of the repository. If you were to do a regular pull request then this makes sense as you'd be bringing your changes into the upstream version. But in this case we want to flip the direction and pull changes from the original version to our fork.
  3. Change the base fork to your repository
  4. You're now back to your fork but you've also asked to compare two identical repositories so GitHub thinks you care about branches not forks. Click on compare across forks to get back your base fork option.
  5. Change the head fork to the upstream (original) repository
    Note at this point that if you were in control of everything at GitHub you would probably just have put a little button in between the base for and head fork that would have flipped them for you rather than doing all these clicks. You might even had added in a separate button to check for upstream changes! But they do know that this is a bit of a silly hack and they're working hard on a bunch of other cool stuff so we'll let them off this one.
  6. You'll see one of two options:
    1. "There isn’t anything to compare" This means you're up to date and you don't have to do anything. Phew. 😌
      1.A list of commits. These are the changes that have happened in the repository since you forked your version. Go to step 7.
      Create a pull request
  7. Note that this pull request is to you! So you can confirm that it's ok and merge it when necessary. And if there are any merge conflicts then it's up to you to figure out what's gone wrong and sort them out.

And now you're ready to continue working on your fork 😃 💥 🎉

Well done!

阅读原文

Golang JSON 时间戳编解码

问题背景

Golang json unmarshal time的格式是RFC3399:2019-04-11T11:41:50.723815+08:00
但是有些客户端不支持RFC3399,2019-04-11 11:41:50这样的时间格式更为通用

解决方案

其实Golang标准库time.Time 中也是实现了json.Marshalerjson.Unmarshaler这两个接口

我们需要参考time.Time的这两个方法自定义一个数据类型: type DateTime time.Time
DateTime也需实现以上两个接口。

之后在需要time.Time 的地方改用Datetime

完整代码

package utils

import "time"

const (
	DatetimeFormat = "2006-01-02 15:04:05"
)

type DateTime time.Time

func (t DateTime)MarshalJSON()([]byte,error){
	b := make([]byte, 0, len(DatetimeFormat)+2)
	b = append(b, '"')
	b = time.Time(t).AppendFormat(b, DatetimeFormat)
	b = append(b, '"')
	return b, nil
}

func (t *DateTime)UnmarshalJSON(data []byte)error{
         if string(data) == "null" {
		return nil
	}
	now, err := time.Parse(`"`+DatetimeFormat+`"`, string(data))
	*t = DateTime(now)
	return err
}

func (t DateTime) String() string {
	return time.Time(t).Format(DatetimeFormat)
}

测试代码

type JsonBody struct {
	Created DateTime
}

func TestDateTime(t *testing.T) {
	jsonBody:=JsonBody{
		Created:DateTime(time.Now()),
	}
	b,_:=json.Marshal(jsonBody)
	fmt.Println("encoded:",string(b))
	//output encoded: {"Created":"2019-04-11 12:52:00"}
	json.Unmarshal(b,jsonBody)
	fmt.Printf("%+v",jsonBody)
	//output {Created:2019-04-11 12:52:00}
}

删除Github仓库所有commit记录(译)

新建orphan分支

git checkout --orphan latest_branch

将需要的文件添加到暂存区

git add -A

Commit

git commit -m "..."

删除master分支

git branch -D master

将当前latest_branch分支重命名为master分支

git branch -m master

最后,推送到远程仓库

git push -f origin master


原文

Golang 笔记

make slice

  • numbers := make(int[],len,cap)
  • len是切片的初始长度,对每一个索引赋值0
  • cap是切片容量,如果切片长度超过了cap,底层重分配空间
  • 如果一开始知道切片的可能长度,最好指定cap,减少内存重分配次数

JSON默认值

To unmarshal JSON into an interface value, Unmarshal stores one of these in the interface value:

注意: JSON numbers <=> Go float64

bool, for JSON booleans
float64, for JSON numbers
string, for JSON strings
[]interface{}, for JSON arrays
map[string]interface{}, for JSON objects
nil for JSON null

ssh 免密登录

ssh 远程登录服务器:ssh username@ip:port 会提示输入密码,比较麻烦
以下尝试用 ssh key 登录服务器,不需要输入密码

在本机上生成 ssh key

首先检查以下本机是否已经存在 ssh key

ssh key 一般是存储在 ~/.ssh 目录
如果没有生成过 ssh key 可以用命令:ssh-keygen -t rsa生成 key。
ssh-keygen 文档

将本机公钥发送到远程服务器

  1. 用密码登录到远程服务器
  2. 复制本机公钥,粘贴到远程服务器~/.ssh/authorized_keys 文件末尾(如果没有该文件,新建一个)
  3. 将.ssh 目录的权限为 700
  4. 将 authorized_keys 目录的权限为 600
    注意: 最后两步不能省略,否则不会生效

测试免密登录

  1. exit 退出远程服务器
  2. ssh username@ip:port如果没有问题的话执行该命令就不需要密码了

设置别名

要记住 IP 也是挺麻烦的
可以为ssh username@ip:port设置别名

PHP 笔记

<?php

//PHP

var_dump(0 == 'all');
//>> true
var_dump('0' == 'all');
//>> false

//⚠️Never cast an unknown fraction to integer
var_dump(intval(0.58 * 100));
//>> int(57)

//the internal representation will be something like 7.9999999999999991118....
var_dump(floor((0.1 + 0.7) * 10));
//>> double(7)

//⚠️不要直接比较浮点数是否相等
var_dump((0.1 + 0.7) == 0.8);
//>> bool(false)


//The key can either be an integer or a string.
//Strings containing valid integers will be cast to the integer type
//⚠️不要用浮点数做key
var_dump([
    1    => "a",
    "1"  => "b",
    1.5  => "c",
    true => "d",
]);
//array(1) {
//    [1] =>
//  string(1) "d"
//}
//As all the keys in the above example are cast to 1,
// the value will be overwritten on every new element
// and the last assigned value "d" is the only one left over.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.