我是动态创建ets表,所以最好避免使用 atom 作为名称。
简单使用字符串作为名称,例如:
:ets.new("aaa", [:named_table])
但是不能编译:
** (ArgumentError) argument error
(stdlib) :ets.new("aaa", [])
Run Code Online (Sandbox Code Playgroud)
如果您正在动态创建 ETS 表,一种方法是将它们创建为未命名的表,并使用 返回的表 ID:ets.new来访问它们:
iex(1)> table1 = :ets.new(:foo, [])
8212
iex(2)> table2 = :ets.new(:foo, [])
12309
iex(3)> :ets.insert(table1, {:a, 1})
true
iex(4)> :ets.insert(table2, {:a, 2})
true
iex(5)> :ets.lookup(table1, :a)
[a: 1]
iex(6)> :ets.lookup(table2, :a)
[a: 2]
Run Code Online (Sandbox Code Playgroud)
(在 Erlang/OTP 20.0 中,表 id 是引用而不是整数,但它的工作方式相同;请参阅此问题。)