第一次写go,看看我都写了啥
2019-08-14 本文已影响0人
卖火柴的wxx
说来丢人,本人第一次写go,写的歪歪扭扭。在此之前,总是到处看到go的大名。感觉go是各种语言混杂,既有js那样定义变量var,const,又有c和c++的指针,解引用和结构体。本智障学艺不精,只是很表面的看法哈哈...网上查了下它的好处,我来瞎说一下是快(相对于Python), 简单(相对于c/c++)...同步并发啥的还没感受到。这里贴一下我用GO连AWS elasticache的redis的code,作为保存。
package main
import (
"fmt"
_ "strings"
_ "time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/elasticache"
)
var servicePortMap map[string]string
func NewConfig() (*elasticache.ElastiCache) {
sess, _ := session.NewSessionWithOptions(session.Options{
Profile: "YOUR-LOCAL-PROFILE-HERE",
})
svc := elasticache.New(sess, &aws.Config{
Region: aws.String("us-west-2"),
})
return svc
}
func GetReplicaGroup() {
svc := NewConfig()
input := &elasticache.DescribeReplicationGroupsInput{
//ReplicationGroupId: aws.String("staging-chat-service"),
}
result, err := svc.DescribeReplicationGroups(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case elasticache.ErrCodeReplicationGroupNotFoundFault:
fmt.Println(elasticache.ErrCodeReplicationGroupNotFoundFault, aerr.Error())
case elasticache.ErrCodeInvalidParameterValueException:
fmt.Println(elasticache.ErrCodeInvalidParameterValueException, aerr.Error())
case elasticache.ErrCodeInvalidParameterCombinationException:
fmt.Println(elasticache.ErrCodeInvalidParameterCombinationException, aerr.Error())
default:
fmt.Println(aerr.Error())
}
} else {
fmt.Println(err.Error())
}
return
}
fmt.Println(result.ReplicationGroups[0].ReplicationGroupId)
for i := range result.ReplicationGroups {
item := result.ReplicationGroups[i]
if item.ConfigurationEndpoint != nil{ //clustered
configurationEndpoint := fmt.Sprint(*item.ConfigurationEndpoint.Address, ":", *item.ConfigurationEndpoint.Port)
servicePortMap[*item.ReplicationGroupId] = configurationEndpoint
} else if item.NodeGroups[0].PrimaryEndpoint != nil {
primaryEndPort := fmt.Sprint(*item.NodeGroups[0].PrimaryEndpoint.Address, ":", *item.NodeGroups[0].PrimaryEndpoint.Port)
servicePortMap[*item.ReplicationGroupId] = primaryEndPort
}
}
}
func GetCacheClusters() {
svc := NewConfig()
input := &elasticache.DescribeCacheClustersInput{
ShowCacheNodeInfo: aws.Bool(true),
ShowCacheClustersNotInReplicationGroups: aws.Bool(true),
}
result, err := svc.DescribeCacheClusters(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case elasticache.ErrCodeCacheClusterNotFoundFault:
fmt.Println(elasticache.ErrCodeCacheClusterNotFoundFault, aerr.Error())
case elasticache.ErrCodeInvalidParameterValueException:
fmt.Println(elasticache.ErrCodeInvalidParameterValueException, aerr.Error())
case elasticache.ErrCodeInvalidParameterCombinationException:
fmt.Println(elasticache.ErrCodeInvalidParameterCombinationException, aerr.Error())
default:
fmt.Println(aerr.Error())
}
} else {
fmt.Println(err.Error())
}
return
}
for i := range result.CacheClusters {
item := result.CacheClusters[i]
if *item.Engine == "redis" { //else memcached; ignore it
servicePortMap[*item.CacheClusterId] = *item.CacheClusterId
}
}
}
func main(){
servicePortMap = make(map[string]string)
GetReplicaGroup()
GetCacheClusters()
fmt.Println(servicePortMap)
}
哈哈这就是第一次写GO写了一晚上的内容,AWS-SDK的api文档写的挺详细。遇到的几个小坑或者说要优化的地方:
- TODO:用paginator写,可以把所有的都拿到,现在的api比如DescribeCacheClusters,有上限可以拿到100.
-
AWS elasticache redis 我把它分成三种,single node/redis/clustered redis. 前两个其实官方文档都是redis(cluster mode disabled) 但是拿endpoint要用不同的api。来个图 显得我很专业的样子哈哈
偷图.jpg
大概是这样.jpg
以上就是我一晚上的感悟总结,要是用boto3,分分钟就写完了,看起来go适合c/c++写的比较熟的人。写文章能不能涨粉丝呀,要是有粉丝我改天再写个k8s的嘻嘻。