Golang学习---断言

断言

普通断言

  1. 普通断言(可用做类型转换):X是接口类型,T可以是非接口类型,若想断言合法,T必须实现接口X
    1. 若T实现了X,则成功转换,否则抛出panic,程序中断
    2. 语法
    3. c := X.(T)

动态检查(安全)断言

  1. flag是一个bool,若T实现了X,则flag为true,否则为false,其余和普通断言一致,通过bool类型的flag可进行判断,实现对应的操作,若结果过多可结合switch + type进行操作
  2. 为什么说是安全呢?因为就算X的实际类型不是你所断言的类型T,也不会报错从而中断,而会继续执行(若断言失败后,调用T的方法仍会执行,跳转至方法案例)
  3. 语法
  4.  c,flag := X.(T)
  • 但注意:T必须实现了X,否则该断言本身就不合法,直接报错

switch + type判断

// 定义一个函数:专门用来打招呼
func greet(s SayHello){
	s.sayHello()
	switch s.(type){
		case Chinese:
			ch := s.(Chinese)
			ch.niuYangGe()
		case American:
			am := s.(American)
			am.disco()
	}
	fmt.Println("打招呼~~~")
}

代码案例

​
package main
import "fmt"
// 定义一个SayHello接口
type SayHello interface{
  sayHello()
}
// 定义中国人、美国人的结构体
type Chinese struct{
}

type American struct{
}
// 为中国人、美国人实现接口
func (ch Chinese) sayHello(){
  fmt.Println("中国--你好")
}
func (am American) sayHello(){
  fmt.Println("American--hello")
}
//案例一:普通断言
func main(){
  am := American{}
  var s SayHello = am
  c := s.(Chinese)  //抛出panic:类型不同,panic: interface conversion: main.SayHello is main.American, not main.Chinese
  fmt.Println(c)
}
//案例二:安全断言
func main(){
  am := American{} 
  var s SayHello = am
  c,falg := s.(Chinese)
  fmt.Println(c,falg) //程序正常运行:打印 {} false
}

​

方法案例

package main
import "fmt"
// 定义一个SayHello接口
type SayHello interface{
	sayHello()
}
// 定义中国人、美国人的结构体
type Chinese struct{
}

type American struct{
}
// 为中国人、美国人实现接口
func (ch Chinese) sayHello(){
	fmt.Println("中国--你好")
}
func (am American) sayHello(){
	fmt.Println("American--hello")
}

// 为中国人、美国人添加各自特有的方法
func (ch Chinese) niuYangGe(){
	fmt.Println("扭秧歌")
}
func (am American) disco(){
	fmt.Println("野狼disco")
}

// 定义一个函数:专门用来打招呼
func greet(s SayHello){
	s.sayHello()
	// 当调用特有方法时,使用断言
	ch,flag := s.(Chinese)
	ch.niuYangGe()
	fmt.Println(ch,flag,"打招呼~~~")
}
func main(){
	ch := Chinese{}
	am := American{}
	greet(ch)
	greet(am) 
	/* 打印结果:
		中国--你好
		扭秧歌
		{} true 打招呼~~~
		American--hello
		扭秧歌
		{} false 打招呼~~~
	*/
}

记录学习时的知识点和疑惑,若有错误,请大佬们多多指点。

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Go语言中,没有内置的SCP(Secure Copy)功能,但你可以使用第方库来实现SCP功能。一个常用的第三方库是`github.com/pkg/sftp`,它提供了SSH文件传输协议的实现。 要使用该库进行SCP操作,你需要先建立一个SSH连接,然后使用SFTP客户端进行文件传输。下面是一个简单的示例代码: ```go package main import ( "fmt" "golang.org/x/crypto/ssh" "github.com/pkg/sftp" "io" "os" ) func main() { // SSH连接配置 config := &ssh.ClientConfig{ User: "username", Auth: []ssh.AuthMethod{ ssh.Password("password"), }, HostKeyCallback: ssh.InsecureIgnoreHostKey(), } // 建立SSH连接 conn, err := ssh.Dial("tcp", "example.com:22", config) if err != nil { fmt.Println("Failed to connect to the server:", err) return } defer conn.Close() // 创建SFTP客户端 client, err := sftp.NewClient(conn) if err != nil { fmt.Println("Failed to create SFTP client:", err) return } defer client.Close() // 打开本地文件 localFile, err := os.Open("/path/to/local/file") if err != nil { fmt.Println("Failed to open local file:", err) return } defer localFile.Close() // 创建远程文件 remoteFile, err := client.Create("/path/to/remote/file") if err != nil { fmt.Println("Failed to create remote file:", err) return } defer remoteFile.Close() // 将本地文件内容复制到远程文件 _, err = io.Copy(remoteFile, localFile) if err != nil { fmt.Println("Failed to copy file:", err) return } fmt.Println("File copied successfully!") } ``` 请注意,上述示例代码中的`username`、`password`、`example.com:22`、`/path/to/local/file`和`/path/to/remote/file`需要根据实际情况进行替换。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值