I want to let Go infer the element type of a container like arrays or map in its initialization like Julia, C++ and Rust do:
# Julia
dictionary = Dict("a" => "apple", "b" => "banana")
println(typeof(dictionary))
# Dict{String, String}
// C++
#include <unordered_map>
#include <utility>
#include <iostream>
#include <boost/core/demangle.hpp>
int main() {
auto const dictionary = std::unordered_map{
std::make_pair("a", "apple"),
std::make_pair("b", "banana"),
};
auto const name = typeid(dictionary).name();
std::cout << boost::core::demangle(name) << std::endl;
}
// std::unordered_map<char const*, char const*, std::hash<char const*>, std::equal_to<char const*>, std::allocator<std::pair<char const* const, char const*> > >
// Rust
use std::collections::HashMap;
use std::any::type_name_of_val;
fn main() {
let dictionary = HashMap::from([
("a", "apple"),
("b", "banana"),
]);
println!({}, type_name_of_val(&dictionary));
}
// std::collections::hash::map::HashMap<&str, &str>
How to realize it in Go? Is it possible? All most tutorials use string
explicitly or use interface{}
or any
but it does not infer the type:
// Go
package main
import (
"fmt"
"reflect"
)
func main() {
dictionary := map[interface{}]interface{}{"a": "apple", "b": "banana"}
fmt.Println(reflect.TypeOf(dictionary))
}
// map[interface {}]interface {}