-
Notifications
You must be signed in to change notification settings - Fork 157
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
8 changed files
with
190 additions
and
9 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
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,36 @@ | ||
// Copyright 2021 ecodeclub | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package iox_test | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/ecodeclub/ekit/iox" | ||
) | ||
|
||
func ExampleNewJSONReader() { | ||
val := iox.NewJSONReader(User{Name: "Tom"}) | ||
_, err := http.NewRequest(http.MethodPost, "/hello", val) | ||
if err != nil { | ||
fmt.Println(err.Error()) | ||
return | ||
} | ||
fmt.Println("OK") | ||
} | ||
|
||
type User struct { | ||
Name string `json:"name"` | ||
} |
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,31 @@ | ||
// Copyright 2021 ecodeclub | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package list_test | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/ecodeclub/ekit" | ||
"github.com/ecodeclub/ekit/internal/list" | ||
) | ||
|
||
func ExampleNewSkipList() { | ||
l := list.NewSkipList[int](ekit.ComparatorRealNumber[int]) | ||
l.Insert(123) | ||
val, _ := l.Get(0) | ||
fmt.Println(val) | ||
// Output: | ||
// 123 | ||
} |
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,33 @@ | ||
// Copyright 2021 ecodeclub | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package queue_test | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/ecodeclub/ekit" | ||
"github.com/ecodeclub/ekit/internal/queue" | ||
) | ||
|
||
func ExampleNewPriorityQueue() { | ||
// 容量,并且队列里面放的是 int | ||
pq := queue.NewPriorityQueue(10, ekit.ComparatorRealNumber[int]) | ||
_ = pq.Enqueue(10) | ||
_ = pq.Enqueue(9) | ||
val, _ := pq.Dequeue() | ||
fmt.Println(val) | ||
// Output: | ||
// 9 | ||
} |
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
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,38 @@ | ||
// Copyright 2021 ecodeclub | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package syncx_test | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/ecodeclub/ekit/syncx" | ||
) | ||
|
||
func ExampleNewLimitPool() { | ||
p := syncx.NewLimitPool(1, func() int { | ||
return 123 | ||
}) | ||
val, ok := p.Get() | ||
fmt.Println("第一次", val, ok) | ||
val, ok = p.Get() | ||
fmt.Println("第二次", val, ok) | ||
p.Put(123) | ||
val, ok = p.Get() | ||
fmt.Println("第三次", val, ok) | ||
// Output: | ||
// 第一次 123 true | ||
// 第二次 0 false | ||
// 第三次 123 true | ||
} |
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
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,35 @@ | ||
// Copyright 2021 ecodeclub | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package syncx_test | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/ecodeclub/ekit/syncx" | ||
) | ||
|
||
func ExampleNewSegmentKeysLock() { | ||
// 参数就是分多少段,你也可以理解为总共有多少锁 | ||
// 锁越多,并发竞争越低,但是消耗内存; | ||
// 锁越少,并发竞争越高,但是内存消耗少; | ||
lock := syncx.NewSegmentKeysLock(100) | ||
// 对应的还有 TryLock | ||
// RLock 和 RUnlock | ||
lock.Lock("key1") | ||
defer lock.Unlock("key1") | ||
fmt.Println("OK") | ||
// Output: | ||
// OK | ||
} |