Posts

Showing posts from March, 2020

How to creat another branch based on one branch with an older commit

your current branch is : X Branch X has a commit id is <sha1-of-commit> then you use this below command git branch Y <sha1-of-commit> OR git branch Y HEAD~3 HEAD  means (the reference to the) current commit HEAD~1  means (the reference to) 1 commit before HEAD~  ALSO means (the reference to) 1 commit before HEAD~87  means (the reference to) 87 commits before ==> you are creating a new branch called Y with the lastest commit is < sha1-of-commit>

read file in golang

pwd , _ := os . Getwd () jsonFile , err := ioutil . ReadFile ( pwd + "/fakeFundDataJson.json" ) if err != nil { fmt . Println ( err ) } fmt . Println ( "Successfully Opened users.json" ) w . Write ( jsonFile ) w . WriteHeader ( http . StatusOK ) w . Header (). Add ( "Content-type" , "application/json" )

how to recover last commit after reset --hard

Recover that lost commit In your repository, run  git reflog  (If you know what branch the commit was in, use  git reflog <branch> ) Look for the commit message that went missing Find the associated SHA, and run  git show <sha> View your missing code in all its glory Use  git cherry-pick <sha>  to get that lost commit into a new branch

DeepEqual in golang for slice

package main import ( "fmt" "reflect" ) func main() { a := []int{1,2,3} b := []int{1,2,3} fmt.Println(reflect.DeepEqual(a, b))         a = []int{1,2,4} b = []int{1,2,3} fmt.Println(reflect.DeepEqual(a, b))         a = []int{1} b = []int{1,2,3} fmt.Println(reflect.DeepEqual(a, b)) } Note: Only use for testing  // Equal tells whether a and b contain the same elements. // A nil argument is equivalent to an empty slice. func Equal(a, b []int) bool { if len(a) != len(b) { return false } for i, v := range a { if v != b[i] { return false } } return true } a := [2]int{1, 2} b := [2]int{1, 3} fmt.Println(a == b) // false

Chown

chown -R greys:admins important-files It's possible to change just the owner of a file, without specifying a group (which will stay intact): $ chown -R greys important-files

Mockery in golang

This library is used for testing in golang https://github.com/vektra/mockery Installation go get github.com/vektra/mockery/... if happened errors then types go get github.com/vektra/mockery/cmd/mockery package test type Stringer interface { String () string } Run:  mockery -name=Stringer  and the following will be output to  mocks/Stringer.go : package mocks import " github.com/stretchr/testify/mock " type Stringer struct { mock. Mock } func ( m * Stringer ) String () string { ret := m. Called () var r0 string if rf , ok := ret. Get ( 0 ).( func () string ); ok { r0 = rf () } else { r0 = ret. Get ( 0 ).( string ) } return r0 }

git diff

git diff mybranch master -- myfile.cs

Docker file read file config from cloud

#build stage FROM golang:alpine AS build-stage RUN apk --no-cache add build-base git bzr mercurial gcc ADD . /src/app RUN cd /src/app/cmd/timeline && go build -o goapp -mod=vendor # final stage FROM alpine WORKDIR /app COPY --from=build-stage /src/app/cmd/timeline/goapp /app/timeline RUN chmod +x timeline ENV CONFIG_SERVICE_URL http://apitoolinternal.tcbs.com.vn:8000/tcbs-config/v1 ENV SERVICE_NAME tcbs-timeline ENV PROFILE_NAME cloud_sit ENTRYPOINT ./timeline -port "${PORT}" -cfgUrl=${CONFIG_SERVICE_URL} -servName=${SERVICE_NAME} -profileName=${PROFILE_NAME}