Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| fc61b53643 | |||
| 582532caff |
216
ArrayUtils.go
Normal file
216
ArrayUtils.go
Normal file
@@ -0,0 +1,216 @@
|
|||||||
|
package golanghelper
|
||||||
|
|
||||||
|
const (
|
||||||
|
INDEX_NOT_FOUND = -1
|
||||||
|
)
|
||||||
|
|
||||||
|
type ArrayUtils struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
// 以 startIndex 為起始位置來尋找 value 在 bool array 中的位置
|
||||||
|
func (a *ArrayUtils) BoolIndexOf(array []bool, value bool, startIndex int) int {
|
||||||
|
if len(array) == 0 {
|
||||||
|
return INDEX_NOT_FOUND
|
||||||
|
}
|
||||||
|
if startIndex < 0 {
|
||||||
|
startIndex = 0
|
||||||
|
}
|
||||||
|
for i := startIndex; i < len(array); i++ {
|
||||||
|
if value == array[i] {
|
||||||
|
return i
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return INDEX_NOT_FOUND
|
||||||
|
}
|
||||||
|
|
||||||
|
// 以 startIndex 為起始位置來尋找 value 在 byte array 中的位置
|
||||||
|
func (a *ArrayUtils) ByteIndexOf(array []byte, value byte, startIndex int) int {
|
||||||
|
if len(array) == 0 {
|
||||||
|
return INDEX_NOT_FOUND
|
||||||
|
}
|
||||||
|
if startIndex < 0 {
|
||||||
|
startIndex = 0
|
||||||
|
}
|
||||||
|
for i := startIndex; i < len(array); i++ {
|
||||||
|
if value == array[i] {
|
||||||
|
return i
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return INDEX_NOT_FOUND
|
||||||
|
}
|
||||||
|
|
||||||
|
// 以 startIndex 為起始位置來尋找 value 在 float32 array 中的位置
|
||||||
|
func (a *ArrayUtils) Float32IndexOf(array []float32, value float32, startIndex int) int {
|
||||||
|
if len(array) == 0 {
|
||||||
|
return INDEX_NOT_FOUND
|
||||||
|
}
|
||||||
|
if startIndex < 0 {
|
||||||
|
startIndex = 0
|
||||||
|
}
|
||||||
|
for i := startIndex; i < len(array); i++ {
|
||||||
|
if value == array[i] {
|
||||||
|
return i
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return INDEX_NOT_FOUND
|
||||||
|
}
|
||||||
|
|
||||||
|
// 以 startIndex 為起始位置來尋找 value 在 float64 array 中的位置
|
||||||
|
func (a *ArrayUtils) Float64IndexOf(array []float64, value float64, startIndex int) int {
|
||||||
|
if len(array) == 0 {
|
||||||
|
return INDEX_NOT_FOUND
|
||||||
|
}
|
||||||
|
if startIndex < 0 {
|
||||||
|
startIndex = 0
|
||||||
|
}
|
||||||
|
for i := startIndex; i < len(array); i++ {
|
||||||
|
if value == array[i] {
|
||||||
|
return i
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return INDEX_NOT_FOUND
|
||||||
|
}
|
||||||
|
|
||||||
|
// 以 startIndex 為起始位置來尋找 value 在 int8 array 中的位置
|
||||||
|
func (a *ArrayUtils) Int8IndexOf(array []int8, value int8, startIndex int) int {
|
||||||
|
if len(array) == 0 {
|
||||||
|
return INDEX_NOT_FOUND
|
||||||
|
}
|
||||||
|
if startIndex < 0 {
|
||||||
|
startIndex = 0
|
||||||
|
}
|
||||||
|
for i := startIndex; i < len(array); i++ {
|
||||||
|
if value == array[i] {
|
||||||
|
return i
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return INDEX_NOT_FOUND
|
||||||
|
}
|
||||||
|
|
||||||
|
// 以 startIndex 為起始位置來尋找 value 在 int16 array 中的位置
|
||||||
|
func (a *ArrayUtils) Int16IndexOf(array []int16, value int16, startIndex int) int {
|
||||||
|
if len(array) == 0 {
|
||||||
|
return INDEX_NOT_FOUND
|
||||||
|
}
|
||||||
|
if startIndex < 0 {
|
||||||
|
startIndex = 0
|
||||||
|
}
|
||||||
|
for i := startIndex; i < len(array); i++ {
|
||||||
|
if value == array[i] {
|
||||||
|
return i
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return INDEX_NOT_FOUND
|
||||||
|
}
|
||||||
|
|
||||||
|
// 以 startIndex 為起始位置來尋找 value 在 int32 array 中的位置
|
||||||
|
func (a *ArrayUtils) Int32IndexOf(array []int32, value int32, startIndex int) int {
|
||||||
|
if len(array) == 0 {
|
||||||
|
return INDEX_NOT_FOUND
|
||||||
|
}
|
||||||
|
if startIndex < 0 {
|
||||||
|
startIndex = 0
|
||||||
|
}
|
||||||
|
for i := startIndex; i < len(array); i++ {
|
||||||
|
if value == array[i] {
|
||||||
|
return i
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return INDEX_NOT_FOUND
|
||||||
|
}
|
||||||
|
|
||||||
|
// 以 startIndex 為起始位置來尋找 value 在 int64 array 中的位置
|
||||||
|
func (a *ArrayUtils) Int64IndexOf(array []int64, value int64, startIndex int) int {
|
||||||
|
if len(array) == 0 {
|
||||||
|
return INDEX_NOT_FOUND
|
||||||
|
}
|
||||||
|
if startIndex < 0 {
|
||||||
|
startIndex = 0
|
||||||
|
}
|
||||||
|
for i := startIndex; i < len(array); i++ {
|
||||||
|
if value == array[i] {
|
||||||
|
return i
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return INDEX_NOT_FOUND
|
||||||
|
}
|
||||||
|
|
||||||
|
// 以 startIndex 為起始位置來尋找 value 在 string array 中的位置
|
||||||
|
func (a *ArrayUtils) StringIndexOf(array []string, value string, startIndex int) int {
|
||||||
|
if len(array) == 0 {
|
||||||
|
return INDEX_NOT_FOUND
|
||||||
|
}
|
||||||
|
if startIndex < 0 {
|
||||||
|
startIndex = 0
|
||||||
|
}
|
||||||
|
for i := startIndex; i < len(array); i++ {
|
||||||
|
if value == array[i] {
|
||||||
|
return i
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return INDEX_NOT_FOUND
|
||||||
|
}
|
||||||
|
|
||||||
|
// 以 startIndex 為起始位置來尋找 value 在 uint8 array 中的位置
|
||||||
|
func (a *ArrayUtils) Uint8IndexOf(array []uint8, value uint8, startIndex int) int {
|
||||||
|
if len(array) == 0 {
|
||||||
|
return INDEX_NOT_FOUND
|
||||||
|
}
|
||||||
|
if startIndex < 0 {
|
||||||
|
startIndex = 0
|
||||||
|
}
|
||||||
|
for i := startIndex; i < len(array); i++ {
|
||||||
|
if value == array[i] {
|
||||||
|
return i
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return INDEX_NOT_FOUND
|
||||||
|
}
|
||||||
|
|
||||||
|
// 以 startIndex 為起始位置來尋找 value 在 uint16 array 中的位置
|
||||||
|
func (a *ArrayUtils) Uint16IndexOf(array []uint16, value uint16, startIndex int) int {
|
||||||
|
if len(array) == 0 {
|
||||||
|
return INDEX_NOT_FOUND
|
||||||
|
}
|
||||||
|
if startIndex < 0 {
|
||||||
|
startIndex = 0
|
||||||
|
}
|
||||||
|
for i := startIndex; i < len(array); i++ {
|
||||||
|
if value == array[i] {
|
||||||
|
return i
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return INDEX_NOT_FOUND
|
||||||
|
}
|
||||||
|
|
||||||
|
// 以 startIndex 為起始位置來尋找 value 在 uint32 array 中的位置
|
||||||
|
func (a *ArrayUtils) Uint32IndexOf(array []uint32, value uint32, startIndex int) int {
|
||||||
|
if len(array) == 0 {
|
||||||
|
return INDEX_NOT_FOUND
|
||||||
|
}
|
||||||
|
if startIndex < 0 {
|
||||||
|
startIndex = 0
|
||||||
|
}
|
||||||
|
for i := startIndex; i < len(array); i++ {
|
||||||
|
if value == array[i] {
|
||||||
|
return i
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return INDEX_NOT_FOUND
|
||||||
|
}
|
||||||
|
|
||||||
|
// 以 startIndex 為起始位置來尋找 value 在 uint64 array 中的位置
|
||||||
|
func (a *ArrayUtils) Uint64IndexOf(array []uint64, value uint64, startIndex int) int {
|
||||||
|
if len(array) == 0 {
|
||||||
|
return INDEX_NOT_FOUND
|
||||||
|
}
|
||||||
|
if startIndex < 0 {
|
||||||
|
startIndex = 0
|
||||||
|
}
|
||||||
|
for i := startIndex; i < len(array); i++ {
|
||||||
|
if value == array[i] {
|
||||||
|
return i
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return INDEX_NOT_FOUND
|
||||||
|
}
|
||||||
53
FileUtils.go
Normal file
53
FileUtils.go
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
package golanghelper
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
type FileUtils struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *FileUtils) CopyFile(srcFilePath, destFilePath string, isOverwrite bool) (err error) {
|
||||||
|
var srcFileInfo os.FileInfo
|
||||||
|
srcFileInfo, err = os.Stat(srcFilePath)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !srcFileInfo.Mode().IsRegular() {
|
||||||
|
return errors.New(fmt.Sprintf("%s is not a regular file", srcFileInfo.Name()))
|
||||||
|
}
|
||||||
|
|
||||||
|
var isNotExist bool
|
||||||
|
_, err = os.Stat(destFilePath)
|
||||||
|
if err != nil {
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
isNotExist = true
|
||||||
|
} else {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var destFile *os.File
|
||||||
|
if isNotExist == false && isOverwrite == false {
|
||||||
|
return errors.New("File already exists")
|
||||||
|
}
|
||||||
|
|
||||||
|
destFile, err = os.Create(destFilePath)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer destFile.Close()
|
||||||
|
|
||||||
|
sourceFile, err := os.Open(srcFilePath)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer sourceFile.Close()
|
||||||
|
|
||||||
|
_, err = io.Copy(destFile, sourceFile)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user