-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
161 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# [viper](https://github.com/spf13/viper) | ||
Viper是完整的go应用程序配置解决方案。 | ||
Viper is a complete configuration solution for Go application. It is designed to work within an application, | ||
and can handle all types configuration needs and formats. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package profile | ||
|
||
import ( | ||
"log" | ||
"math/big" | ||
"time" | ||
) | ||
|
||
func Duration(invocation time.Time, name string) { | ||
elapsed := time.Since(invocation) | ||
log.Printf("%s lasted %s", name, elapsed) | ||
} | ||
|
||
func main() { | ||
defer Duration(time.Now(), "p1") | ||
x := big.NewInt(2) | ||
y := big.NewInt(1) | ||
for one := big.NewInt(1); x.Sign() > 0; x.Sub(x, one) { | ||
y.Mul(x, y) | ||
} | ||
log.Println(x.Set(y)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
(function demo(){ | ||
function profile() { | ||
window.performance.mark('mark_1') | ||
console.log(`window.performance.mark('mark_1')`, performance.getEntriesByName('mark_1')) | ||
|
||
window.performance.mark('mark_2') | ||
console.log(`window.performance.mark('mark_1')`, performance.getEntriesByName('mark_2')) | ||
|
||
window.performance.mark('mark_1') | ||
console.log(`window.performance.mark('mark_1')`, performance.getEntriesByName('mark_1')) | ||
|
||
performance.getEntriesByName('mark_1') | ||
|
||
|
||
} | ||
|
||
profile() | ||
})() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package proxy_pattern | ||
|
||
import "fmt" | ||
|
||
// IObject to use proxy and to object they must implement same methods | ||
type IObject interface { | ||
ObjDo(action string) | ||
} | ||
|
||
// Object represent real objects which proxy will delegate data | ||
type Object struct { | ||
action string | ||
} | ||
|
||
// ObjDo implement IObject interface and handel's all logic | ||
func (obj *Object) ObjDo(action string) { | ||
fmt.Printf("I am real Object ObjDo. I can, %s", action) | ||
} | ||
|
||
// ProxyObject represent proxy object with intercept action | ||
type ProxyObject struct { | ||
object *Object | ||
} | ||
|
||
// ObjDo are implement IObject and intercept action before send into real object. | ||
func (proxy *ProxyObject) ObjDo(action string) { | ||
if proxy != nil { | ||
proxy.object = new(Object) | ||
} | ||
fmt.Printf("I am in proxy object. intercept %s", action) | ||
if action == "Run" { | ||
proxy.object.ObjDo(action) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
The proxy pattern provides an object that controls access to another object, intercepting all calls. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#!/usr/bin/env bash | ||
|
||
IPADDR=$1 | ||
|
||
valid_ip () { | ||
local ip=$1 | ||
local stat=1 | ||
|
||
if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then | ||
OIFS=$IFS | ||
IFS='.' | ||
ip=($ip) | ||
IFS=$OIFS | ||
[[ ${ip[0]} -le 255 && ${ip[1]} -le 255 \ | ||
&& ${ip[2]} -le 255 && ${ip[3]} -le 255 ]] | ||
stat=$? | ||
fi | ||
return $stat | ||
} | ||
|
||
if ! valid_ip "${IPADDR}";then | ||
echo "ip address error !!! please check '${IPADDR}' is ip address? " | ||
exit 1 | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package semaphore_pattern | ||
|
||
import ( | ||
"errors" | ||
"os" | ||
"time" | ||
) | ||
|
||
var ( | ||
ErrNoTickets = errors.New("semaphore: could not aquire semaphore") | ||
ErrIllegalRelease = errors.New("semaphore: can't release the semaphore without acquiring it first") | ||
) | ||
|
||
type Interface interface { | ||
Acquire() error | ||
Release() error | ||
} | ||
|
||
type implementation struct { | ||
sem chan struct{} | ||
timeout time.Duration | ||
} | ||
|
||
func (s *implementation) Acquire() error { | ||
select { | ||
case s.sem <- struct{}{}: | ||
return nil | ||
case <-time.After(s.timeout): | ||
return ErrNoTickets | ||
} | ||
} | ||
|
||
func (s *implementation) Release() error { | ||
select { | ||
case _ = <-s.sem: | ||
return nil | ||
case <-time.After(s.timeout): | ||
return ErrIllegalRelease | ||
} | ||
} | ||
|
||
func NewSemaphore(tickets int, timeout time.Duration) Interface { | ||
return &implementation{ | ||
sem: make(chan struct{}, tickets), | ||
timeout: timeout, | ||
} | ||
} | ||
|
||
func SemaphoreWithTimeouts() { | ||
tickets, timeout := 0, 3*time.Second | ||
s := NewSemaphore(tickets, timeout) | ||
if err := s.Acquire(); err != nil { | ||
if !errors.Is(err, ErrNoTickets) { | ||
panic(err) | ||
} | ||
os.Exit(1) | ||
} | ||
} |