Cleanup code for Go 1.19

This commit is contained in:
tidwall 2022-09-12 17:06:27 -07:00
parent 023433a963
commit dd11eded5c
21 changed files with 35 additions and 48 deletions

View File

@ -221,7 +221,7 @@ func main() {
} }
subtract = true subtract = true
for i := range testsArr { for i := range testsArr {
if strings.ToLower(testsArr[i]) == strings.ToLower(test[1:]) { if strings.EqualFold(testsArr[i], test[1:]) {
testsArr = append(testsArr[:i], testsArr[i+1:]...) testsArr = append(testsArr[:i], testsArr[i+1:]...)
break break
} }

View File

@ -9,12 +9,11 @@ import (
"runtime" "runtime"
"runtime/debug" "runtime/debug"
"github.com/tidwall/resp"
"github.com/yuin/gopher-lua"
"strings" "strings"
)
var errCmdNotSupported = errors.New("command not supported in scripts") "github.com/tidwall/resp"
lua "github.com/yuin/gopher-lua"
)
func Sha1Sum(s string) string { func Sha1Sum(s string) string {
h := sha1.New() h := sha1.New()
@ -271,7 +270,7 @@ func testLua() {
start = runMany(luaState, start, 100) start = runMany(luaState, start, 100)
printMemStats() printMemStats()
start = runMany(luaState, start, 1000) _ = runMany(luaState, start, 1000)
printMemStats() printMemStats()
luaState.Close() luaState.Close()

View File

@ -4,7 +4,6 @@ import (
"flag" "flag"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"net" "net"
"net/http" "net/http"
_ "net/http/pprof" _ "net/http/pprof"
@ -108,7 +107,7 @@ Developer Options:
log.Fatal(err) log.Fatal(err)
} }
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
data, err := ioutil.ReadAll(r.Body) data, err := io.ReadAll(r.Body)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
@ -294,7 +293,7 @@ Developer Options:
var logw io.Writer = os.Stderr var logw io.Writer = os.Stderr
if quiet { if quiet {
logw = ioutil.Discard logw = io.Discard
} }
log.SetOutput(logw) log.SetOutput(logw)
@ -390,7 +389,7 @@ Developer Options:
} }
defer pidcleanup() defer pidcleanup()
if pidfile != "" { if pidfile != "" {
ioutil.WriteFile(pidfile, []byte(fmt.Sprintf("%d\n", os.Getpid())), 0666) os.WriteFile(pidfile, []byte(fmt.Sprintf("%d\n", os.Getpid())), 0666)
} }
c := make(chan os.Signal, 1) c := make(chan os.Signal, 1)

View File

@ -1,4 +1,4 @@
// +build ignore //go:build ignore
package core package core

View File

@ -13,5 +13,5 @@ perl -pe '
' commands.go > commands_gen.go ' commands.go > commands_gen.go
# remove the ignore # remove the ignore
sed -i -e 's/\/\/ +build ignore/\/\/ This file was autogenerated. DO NOT EDIT./g' commands_gen.go sed -i -e 's/\/\/go:build ignore/\/\/ This file was autogenerated. DO NOT EDIT./g' commands_gen.go
rm -rf commands_gen.go-e rm -rf commands_gen.go-e

2
go.mod
View File

@ -107,7 +107,7 @@ require (
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
google.golang.org/appengine v1.6.6 // indirect google.golang.org/appengine v1.6.6 // indirect
google.golang.org/genproto v0.0.0-20200825200019-8632dd797987 // indirect google.golang.org/genproto v0.0.0-20200825200019-8632dd797987 // indirect
google.golang.org/protobuf v1.26.0 // indirect google.golang.org/protobuf v1.28.1 // indirect
honnef.co/go/tools v0.0.1-2020.1.4 // indirect honnef.co/go/tools v0.0.1-2020.1.4 // indirect
) )

3
go.sum
View File

@ -689,8 +689,9 @@ google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpAD
google.golang.org/protobuf v1.24.0/go.mod h1:r/3tXBNzIEhYS9I1OUVjXDlt8tc493IdKGjtUeSXeh4= google.golang.org/protobuf v1.24.0/go.mod h1:r/3tXBNzIEhYS9I1OUVjXDlt8tc493IdKGjtUeSXeh4=
google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c= google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0 h1:bxAC2xTBsZGibn2RTntX0oH50xLsqy1OxA9tTL3p/lk=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w=
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

View File

@ -14,6 +14,7 @@ func New(dl time.Time) *Deadline {
} }
// Check the deadline and panic when reached // Check the deadline and panic when reached
//
//go:noinline //go:noinline
func (dl *Deadline) Check() { func (dl *Deadline) Check() {
if dl == nil || dl.unixNano == 0 { if dl == nil || dl.unixNano == 0 {

View File

@ -4,7 +4,6 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"net/http" "net/http"
"time" "time"
) )
@ -54,7 +53,7 @@ func (conn *HTTPConn) Send(msg string) error {
// close the connection to reuse it // close the connection to reuse it
defer resp.Body.Close() defer resp.Body.Close()
// discard response // discard response
if _, err := io.Copy(ioutil.Discard, resp.Body); err != nil { if _, err := io.Copy(io.Discard, resp.Body); err != nil {
return err return err
} }
// Only allow responses with status code 200, 201, and 202 // Only allow responses with status code 200, 201, and 202

View File

@ -5,7 +5,6 @@ import (
"crypto/x509" "crypto/x509"
"errors" "errors"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"sync" "sync"
"time" "time"
@ -192,7 +191,7 @@ func loadClientTLSCert(KeyFile, CertFile string) ([]tls.Certificate, error) {
func loadRootTLSCert(CACertFile string) (x509.CertPool, error) { func loadRootTLSCert(CACertFile string) (x509.CertPool, error) {
// Load CA cert // Load CA cert
caCert, err := ioutil.ReadFile(CACertFile) caCert, err := os.ReadFile(CACertFile)
if err != nil { if err != nil {
return x509.CertPool{}, err return x509.CertPool{}, err

View File

@ -4,8 +4,8 @@ import (
"crypto/tls" "crypto/tls"
"crypto/x509" "crypto/x509"
"fmt" "fmt"
"io/ioutil"
"math/rand" "math/rand"
"os"
"sync" "sync"
"time" "time"
@ -76,7 +76,7 @@ func (conn *MQTTConn) Send(msg string) error {
} }
if conn.ep.MQTT.CACertFile != "" { if conn.ep.MQTT.CACertFile != "" {
// Load CA cert // Load CA cert
caCert, err := ioutil.ReadFile(conn.ep.MQTT.CACertFile) caCert, err := os.ReadFile(conn.ep.MQTT.CACertFile)
if err != nil { if err != nil {
return err return err
} }

View File

@ -36,7 +36,6 @@ var ErrBadPattern = errors.New("syntax error in pattern")
// //
// On Windows, escaping is disabled. Instead, '\\' is treated as // On Windows, escaping is disabled. Instead, '\\' is treated as
// path separator. // path separator.
//
func wildcardMatch(pattern, name string) (matched bool, err error) { func wildcardMatch(pattern, name string) (matched bool, err error) {
Pattern: Pattern:
for len(pattern) > 0 { for len(pattern) > 0 {

View File

@ -2,7 +2,7 @@ package log
import ( import (
"bytes" "bytes"
"io/ioutil" "io"
"strings" "strings"
"testing" "testing"
@ -189,7 +189,7 @@ func TestLogJSON(t *testing.T) {
func BenchmarkLogPrintf(t *testing.B) { func BenchmarkLogPrintf(t *testing.B) {
LogJSON = false LogJSON = false
Level = 1 Level = 1
SetOutput(ioutil.Discard) SetOutput(io.Discard)
t.ResetTimer() t.ResetTimer()
for i := 0; i < t.N; i++ { for i := 0; i < t.N; i++ {
Printf("X %s", "Y") Printf("X %s", "Y")
@ -208,7 +208,7 @@ func BenchmarkLogJSONPrintf(t *testing.B) {
logger := zap.New( logger := zap.New(
zapcore.NewCore( zapcore.NewCore(
enc, enc,
zapcore.AddSync(ioutil.Discard), zapcore.AddSync(io.Discard),
zap.DebugLevel, zap.DebugLevel,
)).Sugar() )).Sugar()

View File

@ -2,7 +2,6 @@ package server
import ( import (
"encoding/json" "encoding/json"
"io/ioutil"
"os" "os"
"strconv" "strconv"
"strings" "strings"
@ -71,7 +70,7 @@ type Config struct {
func loadConfig(path string) (*Config, error) { func loadConfig(path string) (*Config, error) {
var json string var json string
data, err := ioutil.ReadFile(path) data, err := os.ReadFile(path)
if err != nil { if err != nil {
if !os.IsNotExist(err) { if !os.IsNotExist(err) {
return nil, err return nil, err
@ -217,7 +216,7 @@ func (config *Config) write(writeProperties bool) {
panic(err) panic(err)
} }
data = append(data, '\n') data = append(data, '\n')
err = ioutil.WriteFile(config.path, data, 0600) err = os.WriteFile(config.path, data, 0600)
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -327,11 +326,11 @@ func (config *Config) setProperty(name, value string, fromLoad bool) error {
config._logConfig = value config._logConfig = value
} }
case ReplicaPriority: case ReplicaPriority:
replicaPriority, err := strconv.ParseUint(value, 10, 64) replicaPriority, err := strconv.ParseInt(value, 10, 64)
if err != nil || replicaPriority < 0 { if err != nil || replicaPriority < 0 {
invalid = true invalid = true
} else { } else {
config._replicaPriority = int64(replicaPriority) config._replicaPriority = replicaPriority
} }
} }
@ -522,9 +521,3 @@ func (config *Config) setReadOnly(v bool) {
config._readOnly = v config._readOnly = v
config.mu.Unlock() config.mu.Unlock()
} }
func (config *Config) logConfig() string {
config.mu.RLock()
v := config._logConfig
config.mu.RUnlock()
return v
}

View File

@ -4,7 +4,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"strconv" "strconv"
"strings" "strings"
"time" "time"
@ -272,7 +271,7 @@ func (s *Server) followStep(host string, port int, followc int) error {
s.mu.Unlock() s.mu.Unlock()
log.Info("caught up") log.Info("caught up")
} }
nullw := ioutil.Discard nullw := io.Discard
for { for {
v, telnet, _, err := conn.rd.ReadMultiBulk() v, telnet, _, err := conn.rd.ReadMultiBulk()
if err != nil { if err != nil {

View File

@ -113,9 +113,7 @@ func (s *Server) cmdSetHook(msg *Message) (
cmsg := &Message{} cmsg := &Message{}
*cmsg = *msg *cmsg = *msg
cmsg.Args = make([]string, len(commandvs)) cmsg.Args = make([]string, len(commandvs))
for i := 0; i < len(commandvs); i++ { copy(cmsg.Args, commandvs)
cmsg.Args[i] = commandvs[i]
}
metas := make([]FenceMeta, 0, len(metaMap)) metas := make([]FenceMeta, 0, len(metaMap))
for key, val := range metaMap { for key, val := range metaMap {
metas = append(metas, FenceMeta{key, val}) metas = append(metas, FenceMeta{key, val})

View File

@ -7,6 +7,7 @@ import (
"github.com/tidwall/tile38/core" "github.com/tidwall/tile38/core"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/collectors"
"github.com/prometheus/client_golang/prometheus/promhttp" "github.com/prometheus/client_golang/prometheus/promhttp"
) )
@ -65,9 +66,9 @@ func (s *Server) MetricsHandler(w http.ResponseWriter, r *http.Request) {
reg := prometheus.NewRegistry() reg := prometheus.NewRegistry()
reg.MustRegister( reg.MustRegister(
prometheus.NewProcessCollector(prometheus.ProcessCollectorOpts{}), collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}),
prometheus.NewGoCollector(), collectors.NewGoCollector(),
prometheus.NewBuildInfoCollector(), collectors.NewBuildInfoCollector(),
cmdDurations, cmdDurations,
s, s,
) )

View File

@ -380,7 +380,7 @@ func (sw *scanWriter) testObject(id string, o geojson.Object, fields []float64)
return ok, true, nf return ok, true, nf
} }
//id string, o geojson.Object, fields []float64, noLock bool // id string, o geojson.Object, fields []float64, noLock bool
func (sw *scanWriter) writeObject(opts ScanWriterParams) bool { func (sw *scanWriter) writeObject(opts ScanWriterParams) bool {
if !opts.noLock { if !opts.noLock {
sw.mu.Lock() sw.mu.Lock()

View File

@ -19,14 +19,13 @@ func PO(x, y float64) *geojson.Point {
return geojson.NewPoint(geometry.Point{X: x, Y: y}) return geojson.NewPoint(geometry.Point{X: x, Y: y})
} }
func BenchmarkFieldMatch(t *testing.B) { func BenchmarkFieldMatch(t *testing.B) {
rand.Seed(time.Now().UnixNano()) rand.Seed(time.Now().UnixNano())
items := make([]testPointItem, t.N) items := make([]testPointItem, t.N)
for i := 0; i < t.N; i++ { for i := 0; i < t.N; i++ {
items[i] = testPointItem{ items[i] = testPointItem{
PO(rand.Float64()*360-180, rand.Float64()*180-90), PO(rand.Float64()*360-180, rand.Float64()*180-90),
[]float64{rand.Float64()*9+1, math.Round(rand.Float64()*30) + 1}, []float64{rand.Float64()*9 + 1, math.Round(rand.Float64()*30) + 1},
} }
} }
sw := &scanWriter{ sw := &scanWriter{

View File

@ -1,4 +1,4 @@
// +build !linux,!darwin //go:build !linux && !darwin
package server package server

View File

@ -1,4 +1,4 @@
// +build linux darwin //go:build linux || darwin
package server package server