github.com/OpsMx/go-app-base@v0.0.24/util/util.go (about) 1 // Copyright 2022 OpsMx, Inc 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package util 16 17 import ( 18 "log" 19 "os" 20 ) 21 22 // Check will log an error using log.Fatal() if err is not nil, otherwise 23 // nothing happens. 24 func Check(err error) { 25 if err != nil { 26 log.Fatal(err) 27 } 28 } 29 30 // GetEnvar will return the envar if set, otherwise the default string provided. 31 func GetEnvar(name string, defaultValue string) string { 32 value, found := os.LookupEnv(name) 33 if !found { 34 return defaultValue 35 } 36 return value 37 } 38 39 // Contains looks inside a slice to see if a specific element exists. 40 // This is not fast, and intended only when the length of the slice 41 // is small as a linear search is used. 42 func Contains[T comparable](elems []T, v T) bool { 43 for _, s := range elems { 44 if v == s { 45 return true 46 } 47 } 48 return false 49 }