go 判断interface、string、int不等于默认值
时间: 2023-02-11 21:02:39 浏览: 83
在 Go 中,要判断 interface、string、int 等类型是否不等于默认值,可以使用相应类型的零值进行比较。
对于 interface{} 类型,零值为 nil。
对于 string 类型,零值为 ""。
对于 int 类型,零值为 0。
示例代码如下:
```
package main
import "fmt"
func main() {
var i interface{}
s := "hello"
n := 1
fmt.Println(i != nil) // true
fmt.Println(s != "") // true
fmt.Println(n != 0) // true
}
```
在上面的代码中,第一行定义了一个 interface{} 类型的变量 i,第二行定义了一个 string 类型的变量 s,第三行定义了一个 int 类型的变量 n,第五行、第六行、第七行分别用来判断这三个变量是否不等于默认值。
相关问题
map[string]interface{} 类型断言
在 Go 中,可以使用类型断言来将 `interface{}` 类型转换成具体的类型。对于 `map[string]interface{}`,我们可以使用类型断言来获取其中特定键的值,并将其转换为所需类型。下面是一个示例代码:
```go
m := map[string]interface{}{
"name": "Alice",
"age": 30,
"married": true,
}
// 从 map 中获取 "name" 键的值,并将其转换为字符串
name, ok := m["name"].(string)
if ok {
fmt.Println("Name:", name)
}
// 从 map 中获取 "age" 键的值,并将其转换为整数
age, ok := m["age"].(int)
if ok {
fmt.Println("Age:", age)
}
// 从 map 中获取 "married" 键的值,并将其转换为布尔值
married, ok := m["married"].(bool)
if ok {
fmt.Println("Married:", married)
}
```
在上面的代码中,我们首先定义了一个 `map[string]interface{}`,然后使用类型断言将其中的值转换为具体的类型。注意,在进行类型断言时,我们需要检查转换是否成功,因为如果转换失败,会返回默认值而不是预期的值。
自定义一个Request的注解,包含url(String类型)、scope(int类型、默认值为0),可作用于类和方法定义上,注解可保留至运行时。将自定义注解作用一个测试类,在Main方法中打印该测试类实例对象对应注解的属性
可以这样定义一个自定义的注解:
```java
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
public @interface Request {
String url();
int scope() default 0;
}
```
然后可以创建一个测试类,用自定义的注解进行标注:
```java
@Request(url = "https://www.example.com/api", scope = 1)
public class TestClass {
@Request(url = "https://www.example.com/api2", scope = 2)
public void testMethod() {
// do something
}
}
```
最后,在 `Main` 方法中打印测试类实例对象对应注解的属性:
```java
import java.lang.reflect.Method;
public class Main {
public static void main(String[] args) {
TestClass testClass = new TestClass();
// 获取类上的注解
Request classAnnotation = testClass.getClass().getAnnotation(Request.class);
System.out.println("Class URL: " + classAnnotation.url());
System.out.println("Class Scope: " + classAnnotation.scope());
// 获取方法上的注解
try {
Method method = testClass.getClass().getMethod("testMethod");
Request methodAnnotation = method.getAnnotation(Request.class);
System.out.println("Method URL: " + methodAnnotation.url());
System.out.println("Method Scope: " + methodAnnotation.scope());
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
}
```
运行上述代码,你会得到输出结果:
```
Class URL: https://www.example.com/api
Class Scope: 1
Method URL: https://www.example.com/api2
Method Scope: 2
```
这样就可以通过自定义注解获取类和方法上的属性值了。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)